<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name=Generator content="Microsoft Word 14 (filtered)">

<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Helvetica;
	panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
	{font-family:Courier;
	panose-1:2 7 4 9 2 2 5 2 4 4;}
@font-face
	{font-family:"Tms Rmn";
	panose-1:2 2 6 3 4 5 5 2 3 4;}
@font-face
	{font-family:Helv;
	panose-1:2 11 6 4 2 2 2 3 2 4;}
@font-face
	{font-family:"New York";
	panose-1:2 4 5 3 6 5 6 2 3 4;}
@font-face
	{font-family:System;
	panose-1:0 0 0 0 0 0 0 0 0 0;}
@font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
	{font-family:"MS Mincho";
	panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
	{font-family:Batang;
	panose-1:2 3 6 0 0 1 1 1 1 1;}
@font-face
	{font-family:宋体;
	panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
	{font-family:PMingLiU;
	panose-1:2 2 5 0 0 0 0 0 0 0;}
@font-face
	{font-family:"MS Gothic";
	panose-1:2 11 6 9 7 2 5 8 2 4;}
@font-face
	{font-family:Dotum;
	panose-1:2 11 6 0 0 1 1 1 1 1;}
@font-face
	{font-family:黑体;
	panose-1:2 1 6 9 6 1 1 1 1 1;}
@font-face
	{font-family:MingLiU;
	panose-1:2 2 5 9 0 0 0 0 0 0;}
@font-face
	{font-family:Mincho;
	panose-1:2 2 6 9 4 3 5 8 3 5;}
@font-face
	{font-family:Gulim;
	panose-1:2 11 6 0 0 1 1 1 1 1;}
@font-face
	{font-family:Century;
	panose-1:2 4 6 4 5 5 5 2 3 4;}
@font-face
	{font-family:"Angsana New";
	panose-1:2 2 6 3 5 4 5 2 3 4;}
@font-face
	{font-family:"Cordia New";
	panose-1:2 11 3 4 2 2 2 2 2 4;}
@font-face
	{font-family:Mangal;
	panose-1:2 4 5 3 5 2 3 3 2 2;}
@font-face
	{font-family:Latha;
	panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
	{font-family:Sylfaen;
	panose-1:1 10 5 2 5 3 6 3 3 3;}
@font-face
	{font-family:Vrinda;
	panose-1:2 11 5 2 4 2 4 2 2 3;}
@font-face
	{font-family:Raavi;
	panose-1:2 11 5 2 4 2 4 2 2 3;}
@font-face
	{font-family:Shruti;
	panose-1:2 11 5 2 4 2 4 2 2 3;}
@font-face
	{font-family:Sendnya;
	panose-1:0 0 4 0 0 0 0 0 0 0;}
@font-face
	{font-family:Gautami;
	panose-1:2 11 5 2 4 2 4 2 2 3;}
@font-face
	{font-family:Tunga;
	panose-1:2 11 5 2 4 2 4 2 2 3;}
@font-face
	{font-family:"Estrangelo Edessa";
	panose-1:3 8 6 0 0 0 0 0 0 0;}
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:"Arial Unicode MS";
	panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
	{font-family:Cambria;
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:方正小标宋简体;}
@font-face
	{font-family:"\@黑体";
	panose-1:2 1 6 9 6 1 1 1 1 1;}
@font-face
	{font-family:"\@宋体";
	panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
	{font-family:"\@方正小标宋简体";}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
h1
	{mso-style-link:"标题 1 Char";
	margin-top:17.0pt;
	margin-right:0cm;
	margin-bottom:16.5pt;
	margin-left:7.2pt;
	text-align:center;
	text-indent:-7.2pt;
	page-break-after:avoid;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
h2
	{mso-style-link:"标题 2 Char";
	margin-top:13.0pt;
	margin-right:0cm;
	margin-bottom:13.0pt;
	margin-left:0cm;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:0cm;
	page-break-after:avoid;
	font-size:16.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
h3
	{mso-style-link:"标题 3 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:0cm;
	page-break-after:avoid;
	font-size:14.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
h4
	{mso-style-link:"标题 4 Char";
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:28.8pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-28.8pt;
	page-break-after:avoid;
	font-size:12.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
h5
	{mso-style-link:"标题 5 Char";
	margin-top:14.0pt;
	margin-right:0cm;
	margin-bottom:14.5pt;
	margin-left:36.0pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-36.0pt;
	line-height:156%;
	page-break-after:avoid;
	font-size:14.0pt;
	font-family:宋体;
	font-weight:bold;}
h6
	{mso-style-link:"标题 6 Char";
	margin-top:12.0pt;
	margin-right:0cm;
	margin-bottom:3.2pt;
	margin-left:43.2pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-43.2pt;
	line-height:133%;
	page-break-after:avoid;
	font-size:12.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
p.MsoHeading7, li.MsoHeading7, div.MsoHeading7
	{mso-style-link:"标题 7 Char";
	margin-top:12.0pt;
	margin-right:0cm;
	margin-bottom:3.2pt;
	margin-left:50.4pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-50.4pt;
	line-height:133%;
	page-break-after:avoid;
	font-size:12.0pt;
	font-family:宋体;
	font-weight:bold;}
p.MsoHeading8, li.MsoHeading8, div.MsoHeading8
	{mso-style-link:"标题 8 Char";
	margin-top:12.0pt;
	margin-right:0cm;
	margin-bottom:3.2pt;
	margin-left:57.6pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-72.0pt;
	line-height:133%;
	page-break-after:avoid;
	font-size:12.0pt;
	font-family:"Arial","sans-serif";}
p.MsoHeading9, li.MsoHeading9, div.MsoHeading9
	{mso-style-link:"标题 9 Char";
	margin-top:12.0pt;
	margin-right:0cm;
	margin-bottom:3.2pt;
	margin-left:64.8pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-79.2pt;
	line-height:133%;
	page-break-after:avoid;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.MsoIndex1, li.MsoIndex1, div.MsoIndex1
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:10.5pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex2, li.MsoIndex2, div.MsoIndex2
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex3, li.MsoIndex3, div.MsoIndex3
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:31.5pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex4, li.MsoIndex4, div.MsoIndex4
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:42.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex5, li.MsoIndex5, div.MsoIndex5
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:52.5pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex6, li.MsoIndex6, div.MsoIndex6
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:63.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex7, li.MsoIndex7, div.MsoIndex7
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:73.5pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex8, li.MsoIndex8, div.MsoIndex8
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:84.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoIndex9, li.MsoIndex9, div.MsoIndex9
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:94.5pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-10.5pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoToc1, li.MsoToc1, div.MsoToc1
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;
	font-weight:bold;}
p.MsoToc2, li.MsoToc2, div.MsoToc2
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoToc3, li.MsoToc3, div.MsoToc3
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:42.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoToc4, li.MsoToc4, div.MsoToc4
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:31.5pt;
	margin-bottom:.0001pt;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoToc5, li.MsoToc5, div.MsoToc5
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:42.0pt;
	margin-bottom:.0001pt;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoToc6, li.MsoToc6, div.MsoToc6
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:52.5pt;
	margin-bottom:.0001pt;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoToc7, li.MsoToc7, div.MsoToc7
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:63.0pt;
	margin-bottom:.0001pt;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoToc8, li.MsoToc8, div.MsoToc8
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:73.5pt;
	margin-bottom:.0001pt;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoToc9, li.MsoToc9, div.MsoToc9
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:84.0pt;
	margin-bottom:.0001pt;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoFootnoteText, li.MsoFootnoteText, div.MsoFootnoteText
	{mso-style-link:"脚注文本 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	layout-grid-mode:char;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoCommentText, li.MsoCommentText, div.MsoCommentText
	{mso-style-link:"批注文字 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoHeader, li.MsoHeader, div.MsoHeader
	{mso-style-link:"页眉 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	layout-grid-mode:char;
	border:none;
	padding:0cm;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoFooter, li.MsoFooter, div.MsoFooter
	{mso-style-link:"页脚 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	layout-grid-mode:char;
	font-size:9.0pt;
	font-family:宋体;}
p.MsoIndexHeading, li.MsoIndexHeading, div.MsoIndexHeading
	{mso-style-name:"索引标题\,索引类目\,索引类目1\,索引类目2";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoCaption, li.MsoCaption, div.MsoCaption
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.MsoTof, li.MsoTof, div.MsoTof
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:42.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-21.0pt;
	font-size:10.5pt;
	font-family:宋体;}
span.MsoFootnoteReference
	{vertical-align:super;}
p.MsoList, li.MsoList, div.MsoList
	{margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoList2, li.MsoList2, div.MsoList2
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoList4, li.MsoList4, div.MsoList4
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.MsoDate, li.MsoDate, div.MsoDate
	{mso-style-link:"日期 Char";
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:5.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
a:link, span.MsoHyperlink
	{mso-style-name:"超链接\,超级链接";
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
p
	{mso-style-name:"普通\(网站\)\,普通 \(Web\)\,普通 \(Web\)1\,普通 \(Web\)2\,普通 \(Web\)3";
	margin-right:0cm;
	margin-left:0cm;
	font-size:12.0pt;
	font-family:宋体;}
pre
	{mso-style-name:"HTML 预设格式\,HTML 预先格式化\,HTML 预先格式化1\,HTML 预先格式化2\,HTML 预先格式化3";
	mso-style-link:"HTML 预设格式 Char\,HTML 预先格式化 Char\,HTML 预先格式化1 Char\,HTML 预先格式化2 Char\,HTML 预先格式化3 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:宋体;}
tt
	{font-family:黑体;}
p.MsoCommentSubject, li.MsoCommentSubject, div.MsoCommentSubject
	{mso-style-link:"批注主题 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.5pt;
	font-family:宋体;
	font-weight:bold;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
	{mso-style-link:"批注框文本 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:9.0pt;
	font-family:宋体;}
p.1, li.1, div.1
	{mso-style-name:样式1;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.a, li.a, div.a
	{mso-style-name:代码程序;
	mso-style-link:"代码程序 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:宋体;}
span.Char
	{mso-style-name:"代码程序 Char";
	mso-style-link:代码程序;
	font-family:宋体;}
p.a0, li.a0, div.a0
	{mso-style-name:图说明;
	mso-style-link:"图说明 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
span.Char0
	{mso-style-name:"图说明 Char";
	mso-style-link:图说明;
	font-family:宋体;}
p.0, li.0, div.0
	{mso-style-name:封面0;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:36.0pt;
	font-family:宋体;
	font-weight:bold;}
p.10, li.10, div.10
	{mso-style-name:封面1;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:18.0pt;
	font-family:宋体;
	font-weight:bold;}
p.11, li.11, div.11
	{mso-style-name:非标题1;
	margin-top:7.8pt;
	margin-right:0cm;
	margin-bottom:7.8pt;
	margin-left:0cm;
	text-align:center;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.a1, li.a1, div.a1
	{mso-style-name:文本居中;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.3CharChar, li.3CharChar, div.3CharChar
	{mso-style-name:"图中文字3 Char Char";
	mso-style-link:"图中文字3 Char Char Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:9.0pt;
	font-size:8.0pt;
	font-family:宋体;}
span.3CharCharChar
	{mso-style-name:"图中文字3 Char Char Char";
	mso-style-link:"图中文字3 Char Char";
	font-family:宋体;}
p.post, li.post, div.post
	{mso-style-name:邮件post;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:right;
	line-height:11.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.3, li.3, div.3
	{mso-style-name:图中字体3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.3CharChar1CharCharChar
	{mso-style-name:"图中文字3 Char Char1 Char Char Char";
	font-family:宋体;}
span.3CharChar1CharChar
	{mso-style-name:"图中文字3 Char Char1 Char Char";
	font-family:宋体;}
p.5Char, li.5Char, div.5Char
	{mso-style-name:"图中文字5号 Char";
	mso-style-link:"图中文字5号 Char Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
span.5CharChar
	{mso-style-name:"图中文字5号 Char Char";
	mso-style-link:"图中文字5号 Char";
	font-family:宋体;}
p.5CharChar0, li.5CharChar0, div.5CharChar0
	{mso-style-name:"图中文字小5号 Char Char";
	mso-style-link:"图中文字小5号 Char Char Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
span.5CharCharChar
	{mso-style-name:"图中文字小5号 Char Char Char";
	mso-style-link:"图中文字小5号 Char Char";
	font-family:宋体;}
p.5Char0, li.5Char0, div.5Char0
	{mso-style-name:"图中文字小5号 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.5, li.5, div.5
	{mso-style-name:图中文字小5号;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	layout-grid-mode:char;
	font-size:9.0pt;
	font-family:宋体;}
p.2, li.2, div.2
	{mso-style-name:代码程序2;
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:宋体;}
p.20, li.20, div.20
	{mso-style-name:图说明2;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.21, li.21, div.21
	{mso-style-name:文本居中2;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.3CharCharCharCharChar, li.3CharCharCharCharChar, div.3CharCharCharCharChar
	{mso-style-name:"图中文字3 Char Char Char Char Char";
	mso-style-link:"图中文字3 Char Char Char Char Char Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:9.0pt;
	font-size:8.0pt;
	font-family:宋体;}
span.3CharCharCharCharCharChar
	{mso-style-name:"图中文字3 Char Char Char Char Char Char";
	mso-style-link:"图中文字3 Char Char Char Char Char";
	font-family:宋体;}
p.a2, li.a2, div.a2
	{mso-style-name:图居中;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.110, li.110, div.110
	{mso-style-name:"样式 标题 1 + 居中1";
	margin-right:0cm;
	margin-left:0cm;
	text-align:center;
	page-break-after:avoid;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
span.1Char
	{mso-style-name:"标题 1 Char";
	mso-style-link:"标题 1";
	font-weight:bold;}
p.22, li.22, div.22
	{mso-style-name:"样式 列表 2 + 居中";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.23, li.23, div.23
	{mso-style-name:列表2;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.50, li.50, div.50
	{mso-style-name:图中文字5号;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.111, li.111, div.111
	{mso-style-name:样式11;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.12, li.12, div.12
	{mso-style-name:代码程序1;
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:宋体;}
p.13, li.13, div.13
	{mso-style-name:图说明1;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.01, li.01, div.01
	{mso-style-name:封面01;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:36.0pt;
	font-family:宋体;
	font-weight:bold;}
p.112, li.112, div.112
	{mso-style-name:封面11;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:18.0pt;
	font-family:宋体;
	font-weight:bold;}
p.113, li.113, div.113
	{mso-style-name:非标题11;
	margin-top:7.8pt;
	margin-right:0cm;
	margin-bottom:7.8pt;
	margin-left:0cm;
	text-align:center;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.14, li.14, div.14
	{mso-style-name:文本居中1;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.3Char1, li.3Char1, div.3Char1
	{mso-style-name:"图中文字3 Char1";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:9.0pt;
	font-size:8.0pt;
	font-family:宋体;}
p.post1, li.post1, div.post1
	{mso-style-name:邮件post1;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:right;
	line-height:11.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.31, li.31, div.31
	{mso-style-name:图中字体31;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.5Char1, li.5Char1, div.5Char1
	{mso-style-name:"图中文字5号 Char1";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.5CharChar1, li.5CharChar1, div.5CharChar1
	{mso-style-name:"图中文字小5号 Char Char1";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.5Char10, li.5Char10, div.5Char10
	{mso-style-name:"图中文字小5号 Char1";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.51, li.51, div.51
	{mso-style-name:图中文字小5号1;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.120, li.120, div.120
	{mso-style-name:样式12;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.02, li.02, div.02
	{mso-style-name:封面02;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:36.0pt;
	font-family:宋体;
	font-weight:bold;}
p.121, li.121, div.121
	{mso-style-name:封面12;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:18.0pt;
	font-family:宋体;
	font-weight:bold;}
p.122, li.122, div.122
	{mso-style-name:非标题12;
	margin-top:7.8pt;
	margin-right:0cm;
	margin-bottom:7.8pt;
	margin-left:0cm;
	text-align:center;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.3Char2, li.3Char2, div.3Char2
	{mso-style-name:"图中文字3 Char2";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:9.0pt;
	font-size:8.0pt;
	font-family:宋体;}
p.post2, li.post2, div.post2
	{mso-style-name:邮件post2;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:right;
	line-height:11.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.32, li.32, div.32
	{mso-style-name:图中字体32;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.5Char2, li.5Char2, div.5Char2
	{mso-style-name:"图中文字小5号 Char2";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.52, li.52, div.52
	{mso-style-name:图中文字小5号2;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.3CharCharCharChar, li.3CharCharCharChar, div.3CharCharCharChar
	{mso-style-name:"图中文字3 Char Char Char Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:9.0pt;
	font-size:8.0pt;
	font-family:宋体;}
p.130, li.130, div.130
	{mso-style-name:样式13;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.30, li.30, div.30
	{mso-style-name:代码程序3;
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:宋体;}
p.03, li.03, div.03
	{mso-style-name:封面03;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:36.0pt;
	font-family:宋体;
	font-weight:bold;}
p.131, li.131, div.131
	{mso-style-name:封面13;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:18.0pt;
	font-family:宋体;
	font-weight:bold;}
p.132, li.132, div.132
	{mso-style-name:非标题13;
	margin-top:7.8pt;
	margin-right:0cm;
	margin-bottom:7.8pt;
	margin-left:0cm;
	text-align:center;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.33, li.33, div.33
	{mso-style-name:文本居中3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.3Char3, li.3Char3, div.3Char3
	{mso-style-name:"图中文字3 Char3";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:9.0pt;
	font-size:8.0pt;
	font-family:宋体;}
p.post3, li.post3, div.post3
	{mso-style-name:邮件post3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:right;
	line-height:11.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.330, li.330, div.330
	{mso-style-name:图中字体33;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.5Char20, li.5Char20, div.5Char20
	{mso-style-name:"图中文字5号 Char2";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.5CharChar2, li.5CharChar2, div.5CharChar2
	{mso-style-name:"图中文字小5号 Char Char2";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.5Char3, li.5Char3, div.5Char3
	{mso-style-name:"图中文字小5号 Char3";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.53, li.53, div.53
	{mso-style-name:图中文字小5号3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:9.0pt;
	font-family:宋体;}
p.3Char, li.3Char, div.3Char
	{mso-style-name:"图中文字3 Char";
	mso-style-link:"图中文字3 Char Char5";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:9.0pt;
	font-size:8.0pt;
	font-family:宋体;}
span.3CharChar5
	{mso-style-name:"图中文字3 Char Char5";
	mso-style-link:"图中文字3 Char";
	font-family:宋体;}
p.54, li.54, div.54
	{mso-style-name:图中文字小5紧密;
	margin:0cm;
	margin-bottom:.0001pt;
	line-height:9.0pt;
	text-autospace:ideograph-numeric;
	font-size:9.0pt;
	font-family:宋体;}
p.24, li.24, div.24
	{mso-style-name:居中2号粗宋体;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.15, li.15, div.15
	{mso-style-name:"样式 标题 1 + 居中";
	margin-top:17.0pt;
	margin-right:0cm;
	margin-bottom:16.5pt;
	margin-left:0cm;
	text-align:center;
	page-break-after:avoid;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.25, li.25, div.25
	{mso-style-name:"样式 标题 2 + 行距\: 单倍行距";
	margin-top:13.0pt;
	margin-right:0cm;
	margin-bottom:13.0pt;
	margin-left:28.9pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-28.9pt;
	page-break-after:avoid;
	font-size:16.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
span.2Char
	{mso-style-name:"标题 2 Char";
	mso-style-link:"标题 2";
	font-family:"Arial","sans-serif";
	font-weight:bold;}
p.34, li.34, div.34
	{mso-style-name:列表3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.35, li.35, div.35
	{mso-style-name:表3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.4, li.4, div.4
	{mso-style-name:图说明4;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.40, li.40, div.40
	{mso-style-name:列表4;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.0pt;
	font-size:10.5pt;
	font-family:宋体;}
p.41, li.41, div.41
	{mso-style-name:表4;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.55, li.55, div.55
	{mso-style-name:图说明5;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.56, li.56, div.56
	{mso-style-name:列表5;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.57, li.57, div.57
	{mso-style-name:表5;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.6, li.6, div.6
	{mso-style-name:列表6;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.60, li.60, div.60
	{mso-style-name:表6;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.61, li.61, div.61
	{mso-style-name:图说明6;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.7, li.7, div.7
	{mso-style-name:列表7;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.70, li.70, div.70
	{mso-style-name:图说明7;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.71, li.71, div.71
	{mso-style-name:表7;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.8, li.8, div.8
	{mso-style-name:列表8;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.9, li.9, div.9
	{mso-style-name:列表9;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.90, li.90, div.90
	{mso-style-name:图说明9;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.100, li.100, div.100
	{mso-style-name:列表10;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.101, li.101, div.101
	{mso-style-name:图说明10;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.114, li.114, div.114
	{mso-style-name:列表11;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.115, li.115, div.115
	{mso-style-name:图说明11;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.116, li.116, div.116
	{mso-style-name:表11;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.123, li.123, div.123
	{mso-style-name:列表12;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.124, li.124, div.124
	{mso-style-name:图说明12;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.133, li.133, div.133
	{mso-style-name:图说明13;
	mso-style-link:"图说明13 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
span.13Char
	{mso-style-name:"图说明13 Char";
	mso-style-link:图说明13;
	font-family:宋体;}
p.134, li.134, div.134
	{mso-style-name:列表13;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.26, li.26, div.26
	{mso-style-name:附录2;
	margin-top:13.0pt;
	margin-right:0cm;
	margin-bottom:13.0pt;
	margin-left:0cm;
	text-align:justify;
	text-justify:inter-ideograph;
	page-break-after:avoid;
	font-size:16.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
p.36, li.36, div.36
	{mso-style-name:附录3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	page-break-after:avoid;
	font-size:14.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
span.3Char0
	{mso-style-name:"标题 3 Char";
	mso-style-link:"标题 3";
	font-family:"Arial","sans-serif";
	font-weight:bold;}
p.16, li.16, div.16
	{mso-style-name:附录1;
	margin-top:17.0pt;
	margin-right:0cm;
	margin-bottom:16.5pt;
	margin-left:0cm;
	text-align:center;
	page-break-after:avoid;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.17, li.17, div.17
	{mso-style-name:附录表1;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.42, li.42, div.42
	{mso-style-name:附录4;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	page-break-after:avoid;
	font-size:12.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
span.4Char
	{mso-style-name:"标题 4 Char";
	mso-style-link:"标题 4";
	font-family:"Arial","sans-serif";
	font-weight:bold;}
p.a3, li.a3, div.a3
	{mso-style-name:附录图说明;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.27, li.27, div.27
	{mso-style-name:序标题2;
	margin-top:13.0pt;
	margin-right:0cm;
	margin-bottom:13.0pt;
	margin-left:28.8pt;
	text-align:justify;
	text-justify:inter-ideograph;
	text-indent:-28.8pt;
	page-break-after:avoid;
	font-size:16.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
p.a4, li.a4, div.a4
	{mso-style-name:参考标题;
	margin-top:7.8pt;
	margin-right:0cm;
	margin-bottom:7.8pt;
	margin-left:0cm;
	text-align:center;
	page-break-after:avoid;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.18, li.18, div.18
	{mso-style-name:索引标题1;
	margin-top:7.8pt;
	margin-right:0cm;
	margin-bottom:7.8pt;
	margin-left:0cm;
	text-align:center;
	page-break-after:avoid;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.19, li.19, div.19
	{mso-style-name:列表1;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.25pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.25pt;
	font-size:10.5pt;
	font-family:宋体;}
p.1a, li.1a, div.1a
	{mso-style-name:表1;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.25pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.25pt;
	font-size:10.5pt;
	font-family:宋体;}
p.37, li.37, div.37
	{mso-style-name:图说明3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.62, li.62, div.62
	{mso-style-name:表中字体6号;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	layout-grid-mode:char;
	font-size:7.5pt;
	font-family:宋体;}
p.a5, li.a5, div.a5
	{mso-style-name:正文代码;
	mso-style-link:"正文代码 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char1
	{mso-style-name:"正文代码 Char";
	mso-style-link:正文代码;
	font-family:宋体;}
p.43, li.43, div.43
	{mso-style-name:"样式 标题 4 +";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	page-break-after:avoid;
	font-size:12.0pt;
	font-family:"Arial","sans-serif";
	font-weight:bold;}
p.140, li.140, div.140
	{mso-style-name:表14;
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.25pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.25pt;
	font-size:10.5pt;
	font-family:宋体;}
p.141, li.141, div.141
	{mso-style-name:图说明14;
	mso-style-link:"图说明14 Char";
	margin-top:0cm;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:21.25pt;
	margin-bottom:.0001pt;
	text-align:center;
	text-indent:-21.25pt;
	font-size:10.5pt;
	font-family:宋体;}
span.14Char
	{mso-style-name:"图说明14 Char";
	mso-style-link:图说明14;
	font-family:宋体;}
p.a6, li.a6, div.a6
	{mso-style-name:文件目录表;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.a7, li.a7, div.a7
	{mso-style-name:"样式 正文 +";
	mso-style-link:"样式 正文 + Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char2
	{mso-style-name:"样式 正文 + Char";
	mso-style-link:"样式 正文 +";
	font-family:"Times New Roman","serif";}
p.a8, li.a8, div.a8
	{mso-style-name:表格题注;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.a9, li.a9, div.a9
	{mso-style-name:列表题注;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.aa, li.aa, div.aa
	{mso-style-name:图题注;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.ab, li.ab, div.ab
	{mso-style-name:程序题注;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.ac, li.ac, div.ac
	{mso-style-name:框中文字;
	margin-top:0cm;
	margin-right:21.0pt;
	margin-bottom:0cm;
	margin-left:21.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	border:none;
	padding:0cm;
	font-size:9.0pt;
	font-family:宋体;}
p.125, li.125, div.125
	{mso-style-name:"样式 标题 1 + 居中2";
	margin-top:17.0pt;
	margin-right:0cm;
	margin-bottom:16.5pt;
	margin-left:0cm;
	text-align:center;
	page-break-after:avoid;
	font-size:22.0pt;
	font-family:宋体;
	font-weight:bold;}
p.ad, li.ad, div.ad
	{mso-style-name:"样式 题注 + 宋体 五号 居中";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:黑体;}
p.1b, li.1b, div.1b
	{mso-style-name:序标题1;
	margin-top:17.0pt;
	margin-right:0cm;
	margin-bottom:16.5pt;
	margin-left:0cm;
	line-height:240%;
	page-break-after:avoid;
	font-size:16.0pt;
	font-family:宋体;
	font-weight:bold;}
p.38, li.38, div.38
	{mso-style-name:序标题3;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	page-break-after:avoid;
	font-size:12.0pt;
	font-family:方正小标宋简体;}
p.63, li.63, div.63
	{mso-style-name:表中文字6号;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:7.5pt;
	font-family:宋体;}
p.64, li.64, div.64
	{mso-style-name:图中文字6号左对齐;
	margin:0cm;
	margin-bottom:.0001pt;
	line-height:10.0pt;
	layout-grid-mode:char;
	font-size:7.5pt;
	font-family:宋体;}
p.65, li.65, div.65
	{mso-style-name:图中文字6号;
	mso-style-link:"图中文字6号 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	line-height:10.0pt;
	layout-grid-mode:char;
	font-size:7.5pt;
	font-family:宋体;}
span.6Char
	{mso-style-name:"图中文字6号 Char";
	mso-style-link:图中文字6号;
	font-family:宋体;}
p.ae, li.ae, div.ae
	{mso-style-name:图标;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.28, li.28, div.28
	{mso-style-name:图标2;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
p.af, li.af, div.af
	{mso-style-name:习题标题;
	margin-top:6.0pt;
	margin-right:0cm;
	margin-bottom:0cm;
	margin-left:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	page-break-after:avoid;
	font-size:14.0pt;
	font-family:黑体;}
p.1c, li.1c, div.1c
	{mso-style-name:部分编号1;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:16.0pt;
	font-family:宋体;}
p.af0, li.af0, div.af0
	{mso-style-name:表标题;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.af1, li.af1, div.af1
	{mso-style-name:"样式 题注 + 居中";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:宋体;}
p.ListTitle, li.ListTitle, div.ListTitle
	{mso-style-name:ListTitle;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.FigureTitle, li.FigureTitle, div.FigureTitle
	{mso-style-name:FigureTitle;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.TableTitle, li.TableTitle, div.TableTitle
	{mso-style-name:TableTitle;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.ProgramTitle, li.ProgramTitle, div.ProgramTitle
	{mso-style-name:ProgramTitle;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:center;
	font-size:10.5pt;
	font-family:"Arial","sans-serif";}
p.RightText, li.RightText, div.RightText
	{mso-style-name:RightText;
	margin-top:0cm;
	margin-right:21.0pt;
	margin-bottom:0cm;
	margin-left:42.0pt;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	border:none;
	padding:0cm;
	font-size:9.0pt;
	font-family:宋体;}
p.af2, li.af2, div.af2
	{mso-style-name:表中文字小五;
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:9.0pt;
	font-family:宋体;}
p.af3, li.af3, div.af3
	{mso-style-name:关键词;
	mso-style-link:"关键词 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char3
	{mso-style-name:"关键词 Char";
	mso-style-link:关键词;
	font-family:宋体;}
p.af4, li.af4, div.af4
	{mso-style-name:文件名;
	mso-style-link:"文件名 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char4
	{mso-style-name:"文件名 Char";
	mso-style-link:文件名;
	font-family:宋体;}
p.af5, li.af5, div.af5
	{mso-style-name:选项;
	mso-style-link:"选项 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char5
	{mso-style-name:"选项 Char";
	mso-style-link:选项;
	font-family:宋体;}
p.af6, li.af6, div.af6
	{mso-style-name:命令行;
	mso-style-link:"命令行 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char6
	{mso-style-name:"命令行 Char";
	mso-style-link:命令行;
	font-family:宋体;}
p.af7, li.af7, div.af7
	{mso-style-name:函数名;
	mso-style-link:"函数名 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char7
	{mso-style-name:"函数名 Char";
	mso-style-link:函数名;
	font-family:宋体;}
p.af8, li.af8, div.af8
	{mso-style-name:寄存器名;
	mso-style-link:"寄存器名 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char8
	{mso-style-name:"寄存器名 Char";
	mso-style-link:寄存器名;
	font-family:"Times New Roman","serif";}
p.af9, li.af9, div.af9
	{mso-style-name:变量名;
	mso-style-link:"变量名 Char";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:宋体;}
span.Char9
	{mso-style-name:"变量名 Char";
	mso-style-link:变量名;
	font-family:宋体;}
p.58, li.58, div.58
	{mso-style-name:图中文字小5号左;
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:9.0pt;
	font-family:宋体;}
p.59, li.59, div.59
	{mso-style-name:图中文字小5号靠左;
	margin:0cm;
	margin-bottom:.0001pt;
	layout-grid-mode:char;
	font-size:9.0pt;
	font-family:宋体;}
p.926, li.926, div.926
	{mso-style-name:"样式 代码程序 + 左侧\:  9\.26 厘米";
	margin:0cm;
	margin-bottom:.0001pt;
	layout-grid-mode:char;
	font-size:10.0pt;
	font-family:宋体;}
span.5Char4
	{mso-style-name:"标题 5 Char";
	mso-style-link:"标题 5";
	font-weight:bold;}
span.6Char0
	{mso-style-name:"标题 6 Char";
	mso-style-link:"标题 6";
	font-family:"Arial","sans-serif";
	font-weight:bold;}
span.7Char
	{mso-style-name:"标题 7 Char";
	mso-style-link:"标题 7";
	font-weight:bold;}
span.8Char
	{mso-style-name:"标题 8 Char";
	mso-style-link:"标题 8";
	font-family:"Arial","sans-serif";}
span.9Char
	{mso-style-name:"标题 9 Char";
	mso-style-link:"标题 9";
	font-family:"Arial","sans-serif";}
span.Chara
	{mso-style-name:"脚注文本 Char";
	mso-style-link:脚注文本;
	font-family:宋体;}
span.Charb
	{mso-style-name:"批注文字 Char";
	mso-style-link:批注文字;
	font-family:宋体;}
span.Charc
	{mso-style-name:"页眉 Char";
	mso-style-link:页眉;
	font-family:宋体;}
span.Chard
	{mso-style-name:"页脚 Char";
	mso-style-link:页脚;
	font-family:宋体;}
span.Chare
	{mso-style-name:"日期 Char";
	mso-style-link:日期;
	font-family:宋体;}
span.HTMLChar
	{mso-style-name:"HTML 预设格式 Char\,HTML 预先格式化 Char\,HTML 预先格式化1 Char\,HTML 预先格式化2 Char\,HTML 预先格式化3 Char";
	mso-style-link:"HTML 预设格式\,HTML 预先格式化\,HTML 预先格式化1\,HTML 预先格式化2\,HTML 预先格式化3";
	font-family:宋体;}
span.Charf
	{mso-style-name:"批注主题 Char";
	mso-style-link:批注主题;
	font-family:宋体;
	font-weight:bold;}
span.Charf0
	{mso-style-name:"批注框文本 Char";
	mso-style-link:批注框文本;
	font-family:宋体;}
span.3CharChar1
	{mso-style-name:"图中文字3 Char Char1";
	font-family:宋体;}
span.3CharChar3
	{mso-style-name:"图中文字3 Char Char3";
	font-family:宋体;}
span.msoIns
	{mso-style-name:"";
	text-decoration:underline;
	color:teal;}
span.msoDel
	{mso-style-name:"";
	text-decoration:line-through;
	color:red;}
.MsoChpDefault
	{font-size:10.0pt;}
 /* Page Definitions */
 @page WordSection1
	{size:595.3pt 841.9pt;
	margin:72.0pt 54.0pt 72.0pt 54.0pt;
	layout-grid:15.6pt;}
div.WordSection1
	{page:WordSection1;}
 /* List Definitions */
 ol
	{margin-bottom:0cm;}
ul
	{margin-bottom:0cm;}
-->
</style>

</head>

<body lang=ZH-CN link=blue vlink=purple style='text-justify-trim:punctuation'>

<div class=WordSection1 style='layout-grid:15.6pt'>

<p class=ab><a name="_Toc53320648"><span style='font-family:黑体'>程序</span><span
lang=EN-US>12-6 linux/fs/namei.c</span></a></p>

<div class=a align=center style='text-align:center'><span lang=EN-US>

<hr size=4 width="100%" align=center>

</span></div>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>1</span></u> <b><i>/*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>2</span></u> <b><i>&nbsp;*&nbsp;
linux/fs/namei.c</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>3</span></u> <b><i>&nbsp;*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>4</span></u> <b><i>&nbsp;*&nbsp;
(C) 1991&nbsp; Linus Torvalds</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>5</span></u> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>6</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>7</span></u> <b><i>/*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>8</span></u> <b><i>&nbsp;*
Some corrections by tytso.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp; <u><span style='color:blue'>9</span></u> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>10</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>11</span></u>
#include &lt;linux/sched.h&gt;&nbsp; // </span>调度程序头文件，定义了任务结构<span lang=EN-US>task_struct</span>、任务<span
lang=EN-US>0</span>的数据等。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>12</span></u>
#include &lt;linux/kernel.h&gt; // </span>内核头文件。含有一些内核常用函数的原形定义。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>13</span></u>
#include &lt;asm/segment.h&gt;&nbsp; // </span>段操作头文件。定义了有关段寄存器操作的嵌入式汇编函数。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>14</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>15</span></u>
#include &lt;string.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // </span>字符串头文件。主要定义了一些有关字符串操作的嵌入函数。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>16</span></u>
#include &lt;fcntl.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // </span>文件控制头文件。文件及其描述符的操作控制常数符号的定义。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>17</span></u>
#include &lt;errno.h&gt;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// </span>错误号头文件。包含系统中各种出错号。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>18</span></u>
#include &lt;const.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // </span>常数符号头文件。目前仅定义<span
lang=EN-US>i</span>节点中<span lang=EN-US>i_mode</span>字段的各标志位。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>19</span></u>
#include &lt;sys/stat.h&gt;&nbsp;&nbsp;&nbsp;&nbsp; // </span>文件状态头文件。含有文件或文件系统状态结构<span
lang=EN-US>stat{}</span>和常量。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>20</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>由文件名查找对应<span
lang=EN-US>i</span>节点的内部函数。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>21</span></u>
static struct <u><span style='color:blue'>m_inode</span></u> * <u><span
style='color:blue'>_namei</span></u>(const char * filename, struct <u><span
style='color:blue'>m_inode</span></u> * base,</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>22</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
int follow_links);</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>23</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>下面宏中右侧表达式是访问数组的一种特殊使用方法。它基于这样的一个事实，即用数组名和</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>数组下标所表示的数组项（例如<span
lang=EN-US>a[b]</span>）的值等同于使用数组首指针（地址）加上该项偏移地址</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的形式的值 <span
lang=EN-US>*(a + b)</span>，同时可知项<span lang=EN-US>a[b]</span>也可以表示成<span
lang=EN-US>b[a]</span>的形式。因此对于字符数组项形式</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>为 <span lang=EN-US>&quot;LoveYou&quot;[2]</span>（或者<span
lang=EN-US>2[&quot;LoveYou&quot;]</span>）就等同于<span lang=EN-US>*(&quot;LoveYou&quot;
+ 2)</span>。另外，字符串<span lang=EN-US>&quot;LoveYou&quot;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>在内存中被存储的位置就是其地址，因此数组项<span
lang=EN-US>&quot;LoveYou&quot;[2]</span>的值就是该字符串中索引值为<span lang=EN-US>2</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的字符<span lang=EN-US>&quot;v&quot;</span>所对应的<span
lang=EN-US>ASCII</span>码值<span lang=EN-US>0x76</span>，或用八进制表示就是<span
lang=EN-US>0166</span>。在<span lang=EN-US>C</span>语言中，字符也可以用</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>其<span lang=EN-US>ASCII</span>码值来表示，方法是在字符的<span
lang=EN-US>ASCII</span>码值前面加一个反斜杠。例如字符 <span lang=EN-US>&quot;v&quot;</span>可以表示</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>成<span lang=EN-US>&quot;\x76&quot;</span>或者<span
lang=EN-US>&quot;\166&quot;</span>。因此对于不可显示的字符（例如<span lang=EN-US>ASCII</span>码值为<span
lang=EN-US>0x00--0x1f</span>的控制字符）</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>就可用其<span lang=EN-US>ASCII</span>码值来表示。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>下面是访问模式宏。<span
lang=EN-US>x</span>是头文件<span lang=EN-US>include/fcntl.h</span>中第<span
lang=EN-US>7</span>行开始定义的文件访问（打开）标志。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>这个宏根据文件访问标志<span
lang=EN-US>x</span>的值来索引双引号中对应的数值。双引号中有<span lang=EN-US>4</span>个八进制数值（实</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>际表示<span lang=EN-US>4</span>个控制字符）：<span
lang=EN-US>&quot;\004\002\006\377&quot;</span>，分别表示读、写和执行的权限为<span lang=EN-US>:
r</span>、<span lang=EN-US>w</span>、<span lang=EN-US>rw</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>和<span lang=EN-US>wxrwxrwx</span>，并且分别对应<span
lang=EN-US>x</span>的索引值<span lang=EN-US>0--3</span>。 例如，如果<span lang=EN-US>x</span>为<span
lang=EN-US>2</span>，则该宏返回八进制值<span lang=EN-US>006</span>，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>表示可读可写（<span
lang=EN-US>rw</span>）。另外，其中<span lang=EN-US>O_ACCMODE = 00003</span>，是索引值<span
lang=EN-US>x</span>的屏蔽码。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>24</span></u>
#define <u><span style='color:blue'>ACC_MODE</span></u>(<u><span
style='color:blue'>x</span></u>) (<i>&quot;\004\002\006\377&quot;</i>[(<u><span
style='color:blue'>x</span></u>)&amp;<u><span style='color:blue'>O_ACCMODE</span></u>])</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>25</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>26</span></u> <b><i>/*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>27</span></u> <b><i>&nbsp;*
comment out this line if you want names &gt; NAME_LEN chars to be</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>28</span></u> <b><i>&nbsp;*
truncated. Else they will be disallowed.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>29</span></u> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>如果想让文件名长度<span
lang=EN-US> &gt; NAME_LEN</span>个的字符被截掉，就将下面定义注释掉。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>30</span></u> <b><i>/*
#define NO_TRUNCATE */</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>31</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>32</span></u>
#define <u><span style='color:blue'>MAY_EXEC</span></u> 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>可执行<span lang=EN-US>(</span>可进入<span lang=EN-US>)</span>。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>33</span></u>
#define <u><span style='color:blue'>MAY_WRITE</span></u> 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>可写。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>34</span></u>
#define <u><span style='color:blue'>MAY_READ</span></u> 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>可读。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>35</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>36</span></u> <b><i>/*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>37</span></u> <b><i>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
permission()</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>38</span></u> <b><i>&nbsp;*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>39</span></u> <b><i>&nbsp;*
is used to check for read/write/execute permissions on a file.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>40</span></u> <b><i>&nbsp;*
I don't know if we should look at just the euid or both euid and</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>41</span></u> <b><i>&nbsp;*
uid, but that should be easily changed.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>42</span></u> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp; permission()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>该函数用于检测一个文件的读<span
lang=EN-US>/</span>写<span lang=EN-US>/</span>执行权限。我不知道是否只需检查<span lang=EN-US>euid</span>，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>还是需要检查<span
lang=EN-US>euid</span>和<span lang=EN-US>uid</span>两者，不过这很容易修改。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>检测文件访问许可权限。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>inode
- </span>文件的<span lang=EN-US>i</span>节点指针；<span lang=EN-US>mask - </span>访问属性屏蔽码。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：访问许可返回<span
lang=EN-US>1</span>，否则返回<span lang=EN-US>0</span>。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>43</span></u>
static int <u><span style='color:blue'>permission</span></u>(struct <u><span
style='color:blue'>m_inode</span></u> * inode,int mask)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>44</span></u> {</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>45</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
int mode = inode-&gt;i_mode;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//
</span>文件访问属性。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>46</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>47</span></u> <b><i>/*
special case: not even root can read/write a deleted file */</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /* </span>特殊情况：即使是超级用户（<span
lang=EN-US>root</span>）也不能读<span lang=EN-US>/</span>写一个已被删除的文件<span lang=EN-US>
*/</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果<span lang=EN-US>i</span>节点有对应的设备，但该<span
lang=EN-US>i</span>节点的链接计数值等于<span lang=EN-US>0</span>，表示该文件已被删除，则返回。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>否则，如果进程的有效用户<span
lang=EN-US>id</span>（<span lang=EN-US>euid</span>）与<span lang=EN-US>i</span>节点的用户<span
lang=EN-US>id</span>相同，则取文件宿主的访问权限。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>否则，如果进程的有效组<span
lang=EN-US>id</span>（<span lang=EN-US>egid</span>）与<span lang=EN-US>i</span>节点的组<span
lang=EN-US>id</span>相同，则取组用户的访问权限。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>48</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (inode-&gt;i_dev &amp;&amp; !inode-&gt;i_nlinks)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>49</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>50</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else if (<u><span style='color:blue'>current</span></u>-&gt;euid==inode-&gt;i_uid)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>51</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mode &gt;&gt;= 6;</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>52</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else if (<u><span style='color:blue'>in_group_p</span></u>(inode-&gt;i_gid))</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>53</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mode &gt;&gt;= 3;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>最后判断如果所取的的访问权限与屏蔽码相同，或者是超级用户，则返回<span
lang=EN-US>1</span>，否则返回<span lang=EN-US>0</span>。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>54</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (((mode &amp; mask &amp; 0007) == mask) || <u><span style='color:blue'>suser</span></u>())</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>55</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 1;</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>56</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>57</span></u> }</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>58</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>59</span></u> <b><i>/*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>60</span></u> <b><i>&nbsp;*
ok, we cannot use strncmp, as the name is not in our data space.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>61</span></u> <b><i>&nbsp;*
Thus we'll have to use match. No big problem. Match also makes</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>62</span></u> <b><i>&nbsp;*
some sanity tests.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>63</span></u> <b><i>&nbsp;*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>64</span></u> <b><i>&nbsp;*
NOTE! unlike strncmp, match returns 1 for success, 0 for failure.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>65</span></u> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * ok</span>，我们不能使用<span
lang=EN-US>strncmp</span>字符串比较函数，因为名称不在我们的数据空间</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>（不在内核空间）。 因而我们只能使用
<span lang=EN-US>match()</span>。问题不大，<span lang=EN-US>match()</span>同样</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>也处理一些完整的测试。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>注意！与<span
lang=EN-US>strncmp</span>不同的是<span lang=EN-US>match()</span>成功时返回<span
lang=EN-US>1</span>，失败时返回<span lang=EN-US>0</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>指定长度字符串比较函数。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>len
- </span>比较的字符串长度；<span lang=EN-US>name - </span>文件名指针；<span lang=EN-US>de - </span>目录项结构。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：相同返回<span
lang=EN-US>1</span>，不同返回<span lang=EN-US>0</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>第<span lang=EN-US>68</span>行上定义了一个局部寄存器变量<span
lang=EN-US>same</span>。该变量将被保存在<span lang=EN-US>eax</span>寄存器中，以便于高效</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>访问。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>66</span></u>
static int <u><span style='color:blue'>match</span></u>(int len,const char *
name,struct <u><span style='color:blue'>dir_entry</span></u> * de)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>67</span></u> {</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>68</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
register int same __asm__(<i>&quot;ax&quot;</i>);</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>69</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先判断函数参数的有效性。如果目录项指针空，或者目录项<span
lang=EN-US>i</span>节点等于<span lang=EN-US>0</span>，或者要比较的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>字符串长度超过文件名长度，则返回<span
lang=EN-US>0</span>（不匹配）。如果比较的长度<span lang=EN-US>len</span>等于<span lang=EN-US>0</span>并且目录项</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>中文件名的第<span
lang=EN-US>1</span>个字符是 <span lang=EN-US>'.'</span>，并且只有这么一个字符，那么我们就认为是相同的，因此返</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>回<span lang=EN-US>1</span>（匹配）。如果要比较的长度<span
lang=EN-US>len</span>小于<span lang=EN-US>NAME_LEN</span>，但是目录项中文件名长度超过<span
lang=EN-US>len</span>，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则也返回<span lang=EN-US>0</span>（不匹配）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>第<span lang=EN-US>75</span>行上对目录项中文件名长度是否超过
<span lang=EN-US>len </span>的判断方法是检测 <span lang=EN-US>name[len] </span>是否为<span
lang=EN-US>NULL</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>若长度超过<span lang=EN-US>len</span>，则<span
lang=EN-US>name[len]</span>处就是一个不是<span lang=EN-US>NULL</span>的普通字符。而对于长度为<span
lang=EN-US>len</span>的字符</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>串<span lang=EN-US>name</span>，字符<span
lang=EN-US>name[len]</span>就应该是<span lang=EN-US>NULL</span>。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>70</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!de || !de-&gt;inode || len &gt; <u><span style='color:blue'>NAME_LEN</span></u>)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>71</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>72</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>/* &quot;&quot; means &quot;.&quot; ---&gt; so paths like
&quot;/usr/lib//libc.a&quot; work */</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* &quot;&quot; </span>当作 <span lang=EN-US>&quot;.&quot; </span>来看待 <span
lang=EN-US>---&gt; </span>这样就能处理象 <span lang=EN-US>&quot;/usr/lib//libc.a&quot;
</span>那样的路径名<span lang=EN-US> */</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>73</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!len &amp;&amp; (de-&gt;name[0]==<i>'.'</i>) &amp;&amp; (de-&gt;name[1]==<i>'\0'</i>))</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>74</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 1;</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>75</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (len &lt; <u><span style='color:blue'>NAME_LEN</span></u> &amp;&amp;
de-&gt;name[len])</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>76</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后使用嵌入汇编语句进行快速比较操作。它会在用户数据空间（<span
lang=EN-US>fs</span>段）执行字符串的比较</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>操作。<span lang=EN-US>%0
- eax</span>（比较结果<span lang=EN-US>same</span>）；<span lang=EN-US>%1 - eax</span>（<span
lang=EN-US>eax</span>初值<span lang=EN-US>0</span>）；<span lang=EN-US>%2 - esi</span>（名字指针）；</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // %3 - edi</span>（目录项名指针）；<span
lang=EN-US>%4 - ecx(</span>比较的字节长度值<span lang=EN-US>len)</span>。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>77</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
__asm__(<i>&quot;cld\n\t&quot;</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>清方向标志位。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>78</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<i>&quot;fs ; repe ; cmpsb\n\t&quot;</i>&nbsp;&nbsp; // </span>用户空间执行循环比较<span
lang=EN-US>[esi++]</span>和<span lang=EN-US>[edi++]</span>操作，</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>79</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<i>&quot;setz %%al&quot;</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;// </span>若比较结果一样（<span lang=EN-US>zf=0</span>）则置<span lang=EN-US>al=1</span>（<span
lang=EN-US>same=eax</span>）。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>80</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
:<i>&quot;=a&quot;</i> (same)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>81</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
:<i>&quot;&quot;</i> (0),<i>&quot;S&quot;</i> ((long) name),<i>&quot;D&quot;</i>
((long) de-&gt;name),<i>&quot;c&quot;</i> (len)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>82</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
:<i>&quot;cx&quot;</i>,<i>&quot;di&quot;</i>,<i>&quot;si&quot;</i>);</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>83</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return same;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>返回比较结果。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>84</span></u> }</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>85</span></u> </span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>86</span></u> <b><i>/*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>87</span></u> <b><i>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
find_entry()</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>88</span></u> <b><i>&nbsp;*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>89</span></u> <b><i>&nbsp;*
finds an entry in the specified directory with the wanted name. It</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>90</span></u> <b><i>&nbsp;*
returns the cache buffer in which the entry was found, and the entry</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>91</span></u> <b><i>&nbsp;*
itself (as a parameter - res_dir). It does NOT read the inode of the</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>92</span></u> <b><i>&nbsp;*
entry - you'll have to do that yourself if you want to.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>93</span></u> <b><i>&nbsp;*</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>94</span></u> <b><i>&nbsp;*
This also takes care of the few special cases due to '..'-traversal</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>95</span></u> <b><i>&nbsp;*
over a pseudo-root and a mount point.</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>96</span></u> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;
find_entry()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>在指定目录中寻找一个与名字匹配的目录项。返回一个含有找到目录项的高速</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>缓冲块以及目录项本身（作为一个参数<span
lang=EN-US> - res_dir</span>）。该函数并不读取目录项</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>的<span
lang=EN-US>i</span>节点<span lang=EN-US> - </span>如果需要的话则自己操作。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>由于有<span
lang=EN-US>'..'</span>目录项，因此在操作期间也会对几种特殊情况分别处理<span lang=EN-US> - </span>比如横越</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>一个伪根目录以及安装点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>查找指定目录和文件名的目录项。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>*dir
- </span>指定目录<span lang=EN-US>i</span>节点的指针；<span lang=EN-US>name - </span>文件名；<span
lang=EN-US>namelen - </span>文件名长度；</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>该函数在指定目录的数据（文件）中搜索指定文件名的目录项。并对指定文件名是<span
lang=EN-US>'..'</span>的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>情况根据当前进行的相关设置进行特殊处理。关于函数参数传递指针的指针的作用，请参</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>见<span lang=EN-US>linux/sched.c</span>第<span
lang=EN-US>151</span>行前的注释。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：成功则函数高速缓冲区指针，并在<span
lang=EN-US>*res_dir</span>处返回的目录项结构指针。失败则返回</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>空指针<span lang=EN-US>NULL</span>。</p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>97</span></u>
static struct <u><span style='color:blue'>buffer_head</span></u> * <u><span
style='color:blue'>find_entry</span></u>(struct <u><span style='color:blue'>m_inode</span></u>
** dir,</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>98</span></u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
const char * name, int namelen, struct <u><span style='color:blue'>dir_entry</span></u>
** res_dir)</span></p>

<p class=a><span lang=EN-US>&nbsp;<u><span style='color:blue'>99</span></u> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>100</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int entries;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>101</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int block,i;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>102</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>103</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>104</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>super_block</span></u> * sb;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>105</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>同样，本函数一上来也需要对函数参数的有效性进行判断和验证。如果我们在前面第<span
lang=EN-US>30</span>行</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>定义了符号常数<span
lang=EN-US>NO_TRUNCATE</span>，那么如果文件名长度超过最大长度<span lang=EN-US>NAME_LEN</span>，则不予处理。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果没有定义过<span
lang=EN-US>NO_TRUNCATE</span>，那么在文件名长度超过最大长度<span lang=EN-US>NAME_LEN</span>时截短之。</p>

<p class=a><u><span lang=EN-US style='color:blue'>106</span></u><span
lang=EN-US> #ifdef NO_TRUNCATE</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>107</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (namelen &gt; <u><span
style='color:blue'>NAME_LEN</span></u>)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>108</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>109</span></u><span
lang=EN-US> #else</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>110</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (namelen &gt; <u><span
style='color:blue'>NAME_LEN</span></u>)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>111</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
namelen = <u><span style='color:blue'>NAME_LEN</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>112</span></u><span
lang=EN-US> #endif</span></p>

<p class=a><span lang=EN-US>&nbsp;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先计算本目录中目录项项数<span
lang=EN-US>entries</span>。 目录<span lang=EN-US>i</span>节点 <span lang=EN-US>i_size</span>字段中含有本目录包含的数据</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>长度，因此其除以一个目录项的长度（<span
lang=EN-US>16</span>字节）即可得到该目录中目录项数。然后置空返回</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录项结构指针。</p>

<p class=a><u><span lang=EN-US style='color:blue'>113</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entries =
(*dir)-&gt;i_size / (sizeof (struct <u><span style='color:blue'>dir_entry</span></u>));</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>114</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *res_dir = <u><span
style='color:blue'>NULL</span></u>;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>接下来我们对目录项文件名是<span
lang=EN-US>'..'</span>的情况进行特殊处理。如果当前进程指定的根<span lang=EN-US>i</span>节点就是</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>函数参数指定的目录，则说明对于本进程来说，这个目录就是它的伪根目录，即进程只能访</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>问该目录中的项而不能后退到其父目录中去。也即对于该进程本目录就如同是文件系统的根</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录。因此我们需要将文件名修改为<span
lang=EN-US>'.'</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>否则，如果该目录的<span
lang=EN-US>i</span>节点号等于<span lang=EN-US>ROOT_INO</span>（<span lang=EN-US>1</span>号）的话，说明确实是文件系统的根<span
lang=EN-US>i</span>节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则取文件系统的超级块。如果被安装到的<span
lang=EN-US>i</span>节点存在，则先放回原<span lang=EN-US>i</span>节点，然后对被安装到</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的<span lang=EN-US>i</span>节点进行处理。于是我们让<span
lang=EN-US>*dir</span>指向该被安装到的<span lang=EN-US>i</span>节点；并且该<span lang=EN-US>i</span>节点的引用数加<span
lang=EN-US>1</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>即针对这种情况，我们悄悄地进行了“偷梁换柱”工程<span
lang=EN-US>:)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>115</span></u><span
lang=EN-US> <b><i>/* check for '..', as we might have to do some
&quot;magic&quot; for it */</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /* </span>检查目录项 <span
lang=EN-US>'..'</span>，因为我们可能需要对其进行特殊处理<span lang=EN-US> */</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>116</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (namelen==2
&amp;&amp; <u><span style='color:blue'>get_fs_byte</span></u>(name)==<i>'.'</i>
&amp;&amp; <u><span style='color:blue'>get_fs_byte</span></u>(name+1)==<i>'.'</i>)
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>117</span></u><span
lang=EN-US> <b><i>/* '..' in a pseudo-root results in a faked '.' (just change
namelen) */</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /* </span>伪根中的 <span lang=EN-US>'..'
</span>如同一个假<span lang=EN-US> '.'</span>（只需改变名字长度）<span lang=EN-US> */</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>118</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if ((*dir) == <u><span style='color:blue'>current</span></u>-&gt;root)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>119</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
namelen=1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>120</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else if ((*dir)-&gt;i_num == <u><span style='color:blue'>ROOT_INO</span></u>) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>121</span></u><span
lang=EN-US> <b><i>/* '..' over a mount-point results in 'dir' being exchanged
for the mounted</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>122</span></u><span
lang=EN-US> <b><i>&nbsp;&nbsp;&nbsp;directory-inode. NOTE! We set mounted, so
that we can iput the new dir */</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /* </span>在一个安装点上的 <span
lang=EN-US>'..' </span>将导致目录交换到被安装文件系统的目录<span lang=EN-US>i</span>节点上。注意！</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>由于我们设置了<span
lang=EN-US>mounted</span>标志，因而我们能够放回该新目录<span lang=EN-US> */</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>123</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb=<u><span
style='color:blue'>get_super</span></u>((*dir)-&gt;i_dev);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>124</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (sb-&gt;s_imount) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>125</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(*dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>126</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(*dir)=sb-&gt;s_imount;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>127</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(*dir)-&gt;i_count++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>128</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>129</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>130</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们开始正常操作，查找指定文件名的目录项在什么地方。因此我们需要读取目录的数</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>据，即取出目录<span
lang=EN-US>i</span>节点对应块设备数据区中的数据块（逻辑块）信息。这些逻辑块的块号保</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>存在<span lang=EN-US>i</span>节点结构的
<span lang=EN-US>i_zone[9]</span>数组中。我们先取其中第<span lang=EN-US>1</span>个块号。如果目录<span
lang=EN-US>i</span>节点指向的第</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>一个直接磁盘块号为<span
lang=EN-US>0</span>，则说明该目录竟然不含数据，这不正常。于是返回<span lang=EN-US>NULL</span>退出。否则</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>我们就从节点所在设备读取指定的目录项数据块。当然，如果不成功，则也返回<span
lang=EN-US>NULL</span>退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>131</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(block =
(*dir)-&gt;i_zone[0]))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>132</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>133</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(bh = <u><span
style='color:blue'>bread</span></u>((*dir)-&gt;i_dev,block)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>134</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><span lang=EN-US>&nbsp;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>此时我们就在这个读取的目录<span
lang=EN-US>i</span>节点数据块中搜索匹配指定文件名的目录项。首先让<span lang=EN-US>de</span>指</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>向缓冲块中的数据块部分，并在不超过目录中目录项数的条件下，循环执行搜索。其中<span
lang=EN-US>i</span>是</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录中的目录项索引号，在循环开始时初始化为<span
lang=EN-US>0</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>135</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>136</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de = (struct <u><span
style='color:blue'>dir_entry</span></u> *) bh-&gt;b_data;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>137</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (i &lt;
entries) {</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果当前目录项数据块已经搜索完，还没有找到匹配的目录项，则释放当前目录项数据块。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>再读入目录的下一个逻辑块。若这块为空，则只要还没有搜索完目录中的所有目录项，就</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>跳过该块，继续读目录的下一逻辑块。若该块不空，就让
<span lang=EN-US>de </span>指向该数据块，然后在其中</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>继续搜索。其中<span
lang=EN-US>141</span>行上<span lang=EN-US>i/DIR_ENTRIES_PER_BLOCK</span>可得到当前搜索的目录项所在目录文</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>件中的块号，而<span
lang=EN-US>bmap()</span>函数（<span lang=EN-US>inode.c</span>，第<span lang=EN-US>142</span>行）则可计算出在设备上对应的逻辑块号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>138</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if ((char *)de &gt;= <u><span style='color:blue'>BLOCK_SIZE</span></u>+bh-&gt;b_data)
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>139</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>140</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bh = <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>141</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(block = <u><span style='color:blue'>bmap</span></u>(*dir,i/<u><span
style='color:blue'>DIR_ENTRIES_PER_BLOCK</span></u>)) ||</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>142</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
!(bh = <u><span style='color:blue'>bread</span></u>((*dir)-&gt;i_dev,block))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>143</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
i += <u><span style='color:blue'>DIR_ENTRIES_PER_BLOCK</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>144</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
continue;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>145</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>146</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de = (struct <u><span style='color:blue'>dir_entry</span></u> *) bh-&gt;b_data;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>147</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果找到匹配的目录项的话，则返回该目录项结构指针<span
lang=EN-US>de</span>和该目录项<span lang=EN-US>i</span>节点指针<span lang=EN-US>*dir</span>以</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>及该目录项数据块指针<span
lang=EN-US>bh</span>，并退出函数。否则继续在目录项数据块中比较下一个目录项。</p>

<p class=a><u><span lang=EN-US style='color:blue'>148</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (<u><span style='color:blue'>match</span></u>(namelen,name,de)) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>149</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
*res_dir = de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>150</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>151</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>152</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>153</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
i++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>154</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果指定目录中的所有目录项都搜索完后，还没有找到相应的目录项，则释放目录的数据</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>块，最后返回<span
lang=EN-US>NULL</span>（失败）。</p>

<p class=a><u><span lang=EN-US style='color:blue'>155</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>156</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return <u><span
style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>157</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>158</span></u><span
lang=EN-US> </span></p>

<p class=a><u><span lang=EN-US style='color:blue'>159</span></u><span
lang=EN-US> <b><i>/*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>160</span></u><span
lang=EN-US> <b><i>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add_entry()</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>161</span></u><span
lang=EN-US> <b><i>&nbsp;*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>162</span></u><span
lang=EN-US> <b><i>&nbsp;* adds a file entry to the specified directory, using
the same</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>163</span></u><span
lang=EN-US> <b><i>&nbsp;* semantics as find_entry(). It returns NULL if it
failed.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>164</span></u><span
lang=EN-US> <b><i>&nbsp;*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>165</span></u><span
lang=EN-US> <b><i>&nbsp;* NOTE!! The inode part of 'de' is left at 0 - which
means you</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>166</span></u><span
lang=EN-US> <b><i>&nbsp;* may not sleep between calling this and putting something
into</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>167</span></u><span
lang=EN-US> <b><i>&nbsp;* the entry, as someone else might have used it while
you slept.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>168</span></u><span
lang=EN-US> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp; add_entry()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>使用与<span
lang=EN-US>find_entry()</span>同样的方法，往指定目录中添加一指定文件名的目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>录项。如果失败则返回<span
lang=EN-US>NULL</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>注意！！<span
lang=EN-US>'de'</span>（指定目录项结构指针）的<span lang=EN-US>i</span>节点部分被设置为<span
lang=EN-US>0 - </span>这表</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>示在调用该函数和往目录项中添加信息之间不能去睡眠。
因为如果睡眠，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>那么其他人<span
lang=EN-US>(</span>进程<span lang=EN-US>)</span>可能会使用了该目录项。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>根据指定的目录和文件名添加目录项。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>dir
- </span>指定目录的<span lang=EN-US>i</span>节点；<span lang=EN-US>name - </span>文件名；<span
lang=EN-US>namelen - </span>文件名长度；</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：高速缓冲区指针；<span
lang=EN-US>res_dir - </span>返回的目录项结构指针；</p>

<p class=a><u><span lang=EN-US style='color:blue'>169</span></u><span
lang=EN-US> static struct <u><span style='color:blue'>buffer_head</span></u> * <u><span
style='color:blue'>add_entry</span></u>(struct <u><span style='color:blue'>m_inode</span></u>
* dir,</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>170</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char * name,
int namelen, struct <u><span style='color:blue'>dir_entry</span></u> **
res_dir)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>171</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>172</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int block,i;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>173</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>174</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>175</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>同样，本函数一上来也需要对函数参数的有效性进行判断和验证。如果我们在前面第<span
lang=EN-US>30</span>行</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>定义了符号常数<span
lang=EN-US>NO_TRUNCATE</span>，那么如果文件名长度超过最大长度<span lang=EN-US>NAME_LEN</span>，则不予处理。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果没有定义过<span
lang=EN-US>NO_TRUNCATE</span>，那么在文件名长度超过最大长度<span lang=EN-US>NAME_LEN</span>时截短之。</p>

<p class=a><u><span lang=EN-US style='color:blue'>176</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *res_dir = <u><span
style='color:blue'>NULL</span></u>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>用于返回目录项结构指针。</p>

<p class=a><u><span lang=EN-US style='color:blue'>177</span></u><span
lang=EN-US> #ifdef NO_TRUNCATE</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>178</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (namelen &gt; <u><span
style='color:blue'>NAME_LEN</span></u>)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>179</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>180</span></u><span
lang=EN-US> #else</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>181</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;if (namelen &gt; <u><span
style='color:blue'>NAME_LEN</span></u>)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>182</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
namelen = <u><span style='color:blue'>NAME_LEN</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>183</span></u><span
lang=EN-US> #endif</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们开始操作，向指定目录中添加一个指定文件名的目录项。因此我们需要先读取目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的数据，即取出目录<span
lang=EN-US>i</span>节点对应块设备数据区中的数据块（逻辑块）信息。这些逻辑块的块</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>号保存在<span lang=EN-US>i</span>节点结构的
<span lang=EN-US>i_zone[9]</span>数组中。我们先取其中第<span lang=EN-US>1</span>个块号。如果目录<span
lang=EN-US>i</span>节点指向</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的第一个直接磁盘块号为<span
lang=EN-US>0</span>，则说明该目录竟然不含数据，这不正常。于是返回<span lang=EN-US>NULL</span>退出。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>否则我们就从节点所在设备读取指定的目录项数据块。当然，如果不成功，则也返回<span
lang=EN-US>NULL</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>退出。另外，如果参数提供的文件名长度等于<span
lang=EN-US>0</span>，则也返回<span lang=EN-US>NULL</span>退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>184</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>185</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>186</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(block =
dir-&gt;i_zone[0]))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>187</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>188</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(bh = <u><span
style='color:blue'>bread</span></u>(dir-&gt;i_dev,block)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>189</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>此时我们就在这个目录<span
lang=EN-US>i</span>节点数据块中循环查找最后未使用的空目录项。首先让目录项结构</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>指针<span lang=EN-US>de</span>指向缓冲块中的数据块部分，即第一个目录项处。其中<span
lang=EN-US>i</span>是目录中的目录项索引号，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>在循环开始时初始化为<span
lang=EN-US>0</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>190</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>191</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de = (struct <u><span
style='color:blue'>dir_entry</span></u> *) bh-&gt;b_data;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>192</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (1) {</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果当前目录项数据块已经搜索完毕，但还没有找到需要的空目录项，则释放当前目录项数</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>据块，再读入目录的下一个逻辑块。如果对应的逻辑块不存在就创建一块。若读取或创建操</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>作失败则返回空。如果此次读取的磁盘逻辑块数据返回的缓冲块指针为空，说明这块逻辑块</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>可能是因为不存在而新创建的空块，则把目录项索引值加上一块逻辑块所能容纳的目录项数</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // DIR_ENTRIES_PER_BLOCK</span>，用以跳过该块并继续搜索。否则说明新读入的块上有目录项数据，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>于是让目录项结构指针<span
lang=EN-US>de</span>指向该块的缓冲块数据部分，然后在其中继续搜索。其中<span lang=EN-US>196</span>行</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>上的 <span lang=EN-US>i/DIR_ENTRIES_PER_BLOCK
</span>可计算得到当前搜索的目录项<span lang=EN-US>i </span>所在目录文件中的块号，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>而<span lang=EN-US>create_block()</span>函数（<span
lang=EN-US>inode.c</span>，第<span lang=EN-US>147</span>行）则可读取或创建出在设备上对应的逻辑块。</p>

<p class=a><u><span lang=EN-US style='color:blue'>193</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if ((char *)de &gt;= <u><span style='color:blue'>BLOCK_SIZE</span></u>+bh-&gt;b_data)
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>194</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>195</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bh = <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>196</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
block = <u><span style='color:blue'>create_block</span></u>(dir,i/<u><span
style='color:blue'>DIR_ENTRIES_PER_BLOCK</span></u>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>197</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!block)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>198</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>199</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(bh = <u><span style='color:blue'>bread</span></u>(dir-&gt;i_dev,block)))
{ // </span>若空则跳过该块继续。</p>

<p class=a><u><span lang=EN-US style='color:blue'>200</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
i += <u><span style='color:blue'>DIR_ENTRIES_PER_BLOCK</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>201</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
continue;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>202</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>203</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de = (struct <u><span style='color:blue'>dir_entry</span></u> *) bh-&gt;b_data;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>204</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果当前所操作的目录项序号<span
lang=EN-US>i</span>乘上目录结构大小所的长度值已经超过了该目录<span lang=EN-US>i</span>节点信息</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>所指出的目录数据长度值 <span
lang=EN-US>i_size </span>，则说明整个目录文件数据中没有由于删除文件留下的空</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录项，因此我们只能把需要添加的新目录项附加到目录文件数据的末端处。于是对该处目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>录项进行设置（置该目录项的<span
lang=EN-US>i</span>节点指针为空），并更新该目录文件的长度值（加上一个目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>录项的长度），然后设置目录的<span
lang=EN-US>i</span>节点已修改标志，再更新该目录的改变时间为当前时间。</p>

<p class=a><u><span lang=EN-US style='color:blue'>205</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (i*sizeof(struct <u><span style='color:blue'>dir_entry</span></u>) &gt;=
dir-&gt;i_size) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>206</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de-&gt;inode=0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>207</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir-&gt;i_size = (i+1)*sizeof(struct <u><span style='color:blue'>dir_entry</span></u>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>208</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir-&gt;i_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>209</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir-&gt;i_ctime = <u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>210</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>若当前搜索的目录项 <span
lang=EN-US>de </span>的<span lang=EN-US>i</span>节点为空，则表示找到一个还未使用的空闲目录项或是添加的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>新目录项。于是更新目录的修改时间为当前时间，并从用户数据区复制文件名到该目录项的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>文件名字段，置含有本目录项的相应高速缓冲块已修改标志。返回该目录项的指针以及该高</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>速缓冲块的指针，退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>211</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!de-&gt;inode) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>212</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir-&gt;i_mtime = <u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>213</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
for (i=0; i &lt; <u><span style='color:blue'>NAME_LEN</span></u> ; i++)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>214</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de-&gt;name[i]=(i&lt;namelen)?<u><span style='color:blue'>get_fs_byte</span></u>(name+i):0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>215</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bh-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>216</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
*res_dir = de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>217</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>218</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>219</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;// </span>如果该目录项已经被使用，则继续检测下一个目录项。</p>

<p class=a><u><span lang=EN-US style='color:blue'>220</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
i++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>221</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>本函数执行不到这里。这也许是<span
lang=EN-US>Linus</span>在写这段代码时，先复制了上面<span lang=EN-US>find_entry()</span>函数</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的代码，而后修改成本函数的<span
lang=EN-US style='font-family:Wingdings'>J</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>222</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>223</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return <u><span
style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>224</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>225</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>查找符号链接的<span
lang=EN-US>i</span>节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>dir
- </span>目录<span lang=EN-US>i</span>节点；<span lang=EN-US>inode - </span>目录项<span
lang=EN-US>i</span>节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：返回符号链接到文件的<span
lang=EN-US>i</span>节点指针。出错返回<span lang=EN-US>NULL</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>226</span></u><span
lang=EN-US> static struct <u><span style='color:blue'>m_inode</span></u> * <u><span
style='color:blue'>follow_link</span></u>(struct <u><span style='color:blue'>m_inode</span></u>
* dir, struct <u><span style='color:blue'>m_inode</span></u> * inode)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>227</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>228</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned short fs;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>用于临时保存<span lang=EN-US>fs</span>段寄存器值。</p>

<p class=a><u><span lang=EN-US style='color:blue'>229</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>230</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先判断函数参数的有效性。如果没有给出目录<span
lang=EN-US>i</span>节点，我们就使用进程任务结构中设置的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>根<span lang=EN-US>i</span>节点，并把链接数增<span
lang=EN-US>1</span>。如果没有给出目录项<span lang=EN-US>i</span>节点，则放回目录<span lang=EN-US>i</span>节点后返回<span
lang=EN-US>NULL</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果指定目录项不是一个符号链接，就直接返回目录项对应的<span
lang=EN-US>i</span>节点<span lang=EN-US>inode</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>231</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!dir) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>232</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir = <u><span style='color:blue'>current</span></u>-&gt;root;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>233</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir-&gt;i_count++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>234</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>235</span></u><span
lang=EN-US>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!inode) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>236</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>237</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>238</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>239</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>S_ISLNK</span></u>(inode-&gt;i_mode)) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>240</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>241</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>242</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后取<span lang=EN-US>fs</span>段寄存器值。<span
lang=EN-US>fs</span>通常保存着指向任务数据段的选择符<span lang=EN-US>0x17</span>。如果<span
lang=EN-US>fs</span>没有指向用户</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>数据段，或者给出的目录项<span
lang=EN-US>i</span>节点第<span lang=EN-US>1</span>个直接块块号等于<span lang=EN-US>0</span>，或者是读取第<span
lang=EN-US>1</span>个直接块出错，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则放回<span lang=EN-US>dir</span>和<span
lang=EN-US>inode</span>两个<span lang=EN-US>i</span>节点并返回<span lang=EN-US>NULL</span>退出。否则说明现在<span
lang=EN-US>fs</span>正指向用户数据段、并且</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>我们已经成功地读取了这个符号链接目录项的文件内容，并且文件内容已经在
<span lang=EN-US>bh </span>指向的缓冲</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>块数据区中。实际上，这个缓冲块数据区中仅包含一个链接指向的文件路径名字符串。</p>

<p class=a><u><span lang=EN-US style='color:blue'>243</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __asm__(<i>&quot;mov
%%fs,%0&quot;</i>:<i>&quot;=r&quot;</i> (fs));</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>244</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (fs != 0x17 ||
!inode-&gt;i_zone[0] ||</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>245</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
!(bh = <u><span style='color:blue'>bread</span></u>(inode-&gt;i_dev,
inode-&gt;i_zone[0]))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>246</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>247</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>248</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>249</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>此时我们已经不需要符号链接目录项的<span
lang=EN-US>i</span>节点了，于是把它放回。现在碰到一个问题，那就</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>是内核函数处理的用户数据都是存放在用户数据空间中的，并使用了
<span lang=EN-US>fs </span>段寄存器来从用户</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>空间传递数据到内核空间中。而这里需要处理的数据却在内核空间中。因此为了正确地处理</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>位于内核中的用户数据，我们需要让<span
lang=EN-US>fs</span>段寄存器临时指向内核空间，即让<span lang=EN-US>fs =0x10</span>。并在</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>调用函数处理完后再恢复原<span
lang=EN-US>fs</span>的值。最后释放相应缓冲块，并返回 <span lang=EN-US>_namei()</span>解析得到的符</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>号链接指向的文件<span
lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>250</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u><span
style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>251</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __asm__(<i>&quot;mov
%0,%%fs&quot;</i>::<i>&quot;r&quot;</i> ((unsigned short) 0x10));</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>252</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode = <u><span
style='color:blue'>_namei</span></u>(bh-&gt;b_data,dir,0);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>253</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __asm__(<i>&quot;mov
%0,%%fs&quot;</i>::<i>&quot;r&quot;</i> (fs));</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>254</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>255</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>256</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>257</span></u><span
lang=EN-US> </span></p>

<p class=a><u><span lang=EN-US style='color:blue'>258</span></u><span
lang=EN-US> <b><i>/*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>259</span></u><span
lang=EN-US> <b><i>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get_dir()</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>260</span></u><span
lang=EN-US> <b><i>&nbsp;*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>261</span></u><span
lang=EN-US> <b><i>&nbsp;* Getdir traverses the pathname until it hits the
topmost directory.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>262</span></u><span
lang=EN-US> <b><i>&nbsp;* It returns NULL on failure.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>263</span></u><span
lang=EN-US> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;
get_dir()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>该函数根据给出的路径名进行搜索，直到达到最顶端的目录。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>如果失败则返回<span
lang=EN-US>NULL</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>从指定目录开始搜寻指定路径名的目录（或文件名）的<span
lang=EN-US>i</span>节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>pathname
- </span>路径名；<span lang=EN-US>inode - </span>指定起始目录的<span lang=EN-US>i</span>节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：目录或文件的<span
lang=EN-US>i</span>节点指针。失败时返回<span lang=EN-US>NULL</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>264</span></u><span
lang=EN-US> static struct <u><span style='color:blue'>m_inode</span></u> * <u><span
style='color:blue'>get_dir</span></u>(const char * pathname, struct <u><span
style='color:blue'>m_inode</span></u> * inode)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>265</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>266</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char c;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>267</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char * <u><span
style='color:blue'>thisname</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>268</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>269</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int namelen,inr;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>270</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>271</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>272</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先判断参数有效性。如果给出的指定目录的<span
lang=EN-US>i</span>节点指针<span lang=EN-US>inode</span>为空，则使用当前进程的当</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>前工作目录<span lang=EN-US>i</span>节点。如果用户指定路径名的第<span
lang=EN-US>1</span>个字符是<span lang=EN-US>'/'</span>，则说明路径名是绝对路径名。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则应该从当前进程任务结构中设置的根（或伪根）<span
lang=EN-US>i</span>节点开始操作。于是我们需要先放回参</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>数指定的或者设定的目录<span
lang=EN-US>i</span>节点，并取得进程使用的根<span lang=EN-US>i</span>节点。然后把该<span
lang=EN-US>i</span>节点的引用计数</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>加<span lang=EN-US>1</span>，并删除路径名的第<span
lang=EN-US>1</span>个字符 <span lang=EN-US>'/'</span>。这样就可以保证当前进程只能以其设定的根<span
lang=EN-US>i</span>节点</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>作为搜索的起点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>273</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!inode) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>274</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode = <u><span style='color:blue'>current</span></u>-&gt;pwd;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>进程的当前工作目录<span lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>275</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_count++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>276</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>277</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((c=<u><span
style='color:blue'>get_fs_byte</span></u>(pathname))==<i>'/'</i>) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>278</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>放回原<span lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>279</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode = <u><span style='color:blue'>current</span></u>-&gt;root;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>为进程指定的根<span lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>280</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pathname++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>281</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_count++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>282</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后针对路径名中的各个目录名部分和文件名进行循环处理。在循环处理过程中，我们先要</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>对当前正在处理的目录名部分的 <span
lang=EN-US>i</span>节点进行有效性判断，并且把变量<span lang=EN-US>thisname </span>指向当前正</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>在处理的目录名部分。如果该<span
lang=EN-US>i</span>节点表明当前处理的目录名部分不是目录类型，或者没有可</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>进入该目录的访问许可，则放回该<span
lang=EN-US>i</span>节点，并返回<span lang=EN-US>NULL</span>退出。 当然在刚进入循环时，当前</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的<span lang=EN-US>i</span>节点<span
lang=EN-US>inode</span>就是进程根<span lang=EN-US>i</span>节点或者是当前工作目录的<span
lang=EN-US>i</span>节点，或者是参数指定的某</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>个搜索起始目录的<span
lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>283</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (1) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>284</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>thisname</span></u> = pathname;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>285</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!<u><span style='color:blue'>S_ISDIR</span></u>(inode-&gt;i_mode) || !<u><span
style='color:blue'>permission</span></u>(inode,<u><span style='color:blue'>MAY_EXEC</span></u>))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>286</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u><span
style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>287</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>288</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>每次循环我们处理路径名中一个目录名（或文件名）部分。因此在每次循环中我们都要从路</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>径名字符串中分离出一个目录名（或文件名）。方法是从当前路径名指针
<span lang=EN-US>pathname </span>开始处</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>搜索检测字符，直到字符是一个结尾符（<span
lang=EN-US>NULL</span>）或者是一个<span lang=EN-US>'/'</span>字符。此时变量 <span
lang=EN-US>namelen </span>正</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>好是当前处理目录名部分的长度，而变量 <span
lang=EN-US>thisname </span>正指向该目录名部分的开始处。此时如</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>果字符是结尾符<span
lang=EN-US>NULL</span>，则表明已经搜索到路径名末尾，并已到达最后指定目录名或文件名，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则返回该<span lang=EN-US>i</span>节点指针退出。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>注意！如果路径名中最后一个名称也是一个目录名，但其后面没有加上
<span lang=EN-US>'/'</span>字符，则函数不</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>会返回该最后目录名的<span
lang=EN-US>i</span>节点！例如：对于路径名<span lang=EN-US>/usr/src/linux</span>，该函数将只返回<span
lang=EN-US>src/</span>目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>录名的<span lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>289</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
for(namelen=0;(c=<u><span style='color:blue'>get_fs_byte</span></u>(pathname++))&amp;&amp;(c!=<i>'/'</i>);namelen++)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>290</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>/* nothing */</i></b> ;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>291</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!c)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>292</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return inode;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>在得到当前目录名部分（或文件名）后，我们调用查找目录项函数<span
lang=EN-US>find_entry()</span>在当前处</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>理的目录中寻找指定名称的目录项。如果没有找到，则放回该<span
lang=EN-US>i</span>节点，并返回<span lang=EN-US>NULL</span>退出。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后在找到的目录项中取出其<span
lang=EN-US>i</span>节点号<span lang=EN-US>inr</span>和设备号<span lang=EN-US>idev</span>，释放包含该目录项的高速缓冲</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>块并放回该<span lang=EN-US>i</span>节点。
然后取节点号<span lang=EN-US>inr</span>的<span lang=EN-US>i</span>节点<span lang=EN-US>inode</span>，并以该目录项为当前目录继续循</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>环处理路径名中的下一目录名部分（或文件名）。如果当前处理的目录项是一个符号链接</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>名，则使用<span lang=EN-US>follow_link()</span>就可以得到其指向的目录项名的<span
lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>293</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(bh = <u><span style='color:blue'>find_entry</span></u>(&amp;inode,<u><span
style='color:blue'>thisname</span></u>,namelen,&amp;de))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>294</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>295</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>296</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>297</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inr = de-&gt;inode;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>当前目录名部分的<span lang=EN-US>i</span>节点号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>298</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>299</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir = inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>300</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(inode = <u><span style='color:blue'>iget</span></u>(dir-&gt;i_dev,inr)))
{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // </span>取<span lang=EN-US>i</span>节点内容。</p>

<p class=a><u><span lang=EN-US style='color:blue'>301</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>302</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>303</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>304</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(inode = <u><span style='color:blue'>follow_link</span></u>(dir,inode)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>305</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>306</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>307</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>308</span></u><span
lang=EN-US> </span></p>

<p class=a><u><span lang=EN-US style='color:blue'>309</span></u><span
lang=EN-US> <b><i>/*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>310</span></u><span
lang=EN-US> <b><i>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir_namei()</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>311</span></u><span
lang=EN-US> <b><i>&nbsp;*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>312</span></u><span
lang=EN-US> <b><i>&nbsp;* dir_namei() returns the inode of the directory of the</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>313</span></u><span
lang=EN-US> <b><i>&nbsp;* specified name, and the name within that directory.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>314</span></u><span
lang=EN-US> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp; dir_namei()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * dir_namei()</span>函数返回指定目录名的<span
lang=EN-US>i</span>节点指针，以及在最顶层</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>目录的名称。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>pathname
- </span>目录路径名；<span lang=EN-US>namelen - </span>路径名长度；<span lang=EN-US>name - </span>返回的最顶层目录名。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // base - </span>搜索起始目录的<span
lang=EN-US>i</span>节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：指定目录名最顶层目录的<span
lang=EN-US>i</span>节点指针和最顶层目录名称及长度。出错时返回<span lang=EN-US>NULL</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>注意！！这里“最顶层目录”是指路径名中最靠近末端的目录。</p>

<p class=a><u><span lang=EN-US style='color:blue'>315</span></u><span
lang=EN-US> static struct <u><span style='color:blue'>m_inode</span></u> * <u><span
style='color:blue'>dir_namei</span></u>(const char * pathname,</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>316</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int * namelen,
const char ** name, struct <u><span style='color:blue'>m_inode</span></u> *
base)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>317</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>318</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char c;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>319</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>320</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>321</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先取得指定路径名最顶层目录的<span
lang=EN-US>i</span>节点。然后对路径名<span lang=EN-US>pathname</span>进行搜索检测，查出最后</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>一个<span lang=EN-US>'/'</span>字符后面的名字字符串，计算其长度，并且返回最顶层目录的<span
lang=EN-US>i</span>节点指针。注意！如</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>果路径名最后一个字符是斜杠字符<span
lang=EN-US>'/'</span>，那么返回的目录名为空，并且长度为<span lang=EN-US>0</span>。但返回的<span
lang=EN-US>i</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>节点指针仍然指向最后一个<span
lang=EN-US>'/'</span>字符前目录名的<span lang=EN-US>i</span>节点。参见第<span lang=EN-US>289</span>行上的“注意”说明。</p>

<p class=a><u><span lang=EN-US style='color:blue'>322</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir = <u><span
style='color:blue'>get_dir</span></u>(pathname,base)))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// base</span>是指定的起始目录<span lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>323</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>324</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; basename =
pathname;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>325</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (c=<u><span
style='color:blue'>get_fs_byte</span></u>(pathname++))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>326</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (c==<i>'/'</i>)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>327</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
basename=pathname;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>328</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *namelen =
pathname-basename-1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>329</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *name = basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>330</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return dir;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>331</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>332</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>取指定路径名的<span
lang=EN-US>i</span>节点内部函数。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>pathname
- </span>路径名；<span lang=EN-US>base - </span>搜索起点目录<span lang=EN-US>i</span>节点；<span
lang=EN-US>follow_links - </span>是否跟随</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>符号链接的标志，<span
lang=EN-US>1 - </span>需要，<span lang=EN-US>0</span>不需要。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：对应的<span
lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>333</span></u><span
lang=EN-US> struct <u><span style='color:blue'>m_inode</span></u> * <u><span
style='color:blue'>_namei</span></u>(const char * pathname, struct <u><span
style='color:blue'>m_inode</span></u> * base,</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>334</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int follow_links)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>335</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>336</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>337</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int inr,namelen;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>338</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>339</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>340</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>341</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先查找指定路径名中最顶层目录的目录名并得到其<span
lang=EN-US>i</span>节点。若不存在，则返回<span lang=EN-US>NULL</span>退出。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果返回的最顶层名字的长度是<span
lang=EN-US>0</span>，则表示该路径名以一个目录名为最后一项。因此说明我</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>们已经找到对应目录的<span
lang=EN-US>i</span>节点，可以直接返回该<span lang=EN-US>i</span>节点退出。如果返回的名字长度不是<span
lang=EN-US>0</span>，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则我们以指定的起始目录<span
lang=EN-US>base</span>，再次调用<span lang=EN-US>dir_namei()</span>函数来搜索顶层目录名，并根据返回</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的信息作类似判断。</p>

<p class=a><u><span lang=EN-US style='color:blue'>311</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir = <u><span
style='color:blue'>dir_namei</span></u>(pathname,&amp;namelen,&amp;basename)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>312</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>313</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(!namelen)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>/* special case: '/usr/' etc */</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>314</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return dir;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* </span>对应于<span lang=EN-US>'/usr/'</span>等情况<span lang=EN-US> */</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>342</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(base = <u><span
style='color:blue'>dir_namei</span></u>(pathname,&amp;namelen,&amp;basename,base)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>343</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>344</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(!namelen)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>/* special case: '/usr/' etc */</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>345</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return base;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后在返回的顶层目录中寻找指定文件名目录项的<span
lang=EN-US>i</span>节点。注意！因为如果最后也是一个目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>录名，但其后没有加<span
lang=EN-US>'/'</span>，则不会返回该最后目录的<span lang=EN-US>i</span>节点！ 例如：<span
lang=EN-US>/usr/src/linux</span>，将只</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回 <span lang=EN-US>src/</span>目录名的<span
lang=EN-US>i</span>节点。因为函数<span lang=EN-US>dir_namei() </span>将不以<span
lang=EN-US>'/'</span>结束的最后一个名字当作一个</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>文件名来看待，因此这里需要单独对这种情况使用寻找目录项<span
lang=EN-US>i</span>节点函数<span lang=EN-US>find_entry()</span>进行</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>处理。此时<span lang=EN-US>de</span>中含有寻找到的目录项指针，而<span
lang=EN-US>dir</span>是包含该目录项的目录的<span lang=EN-US>i</span>节点指针。</p>

<p class=a><u><span lang=EN-US style='color:blue'>346</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;base,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>347</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>348</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(base);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>349</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>350</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>接着取该目录项的<span
lang=EN-US>i</span>节点号，并释放包含该目录项的高速缓冲块并放回目录<span lang=EN-US>i</span>节点。然后取</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>对应节点号的<span
lang=EN-US>i</span>节点，修改其被访问时间为当前时间，并置已修改标志。最后返回该<span lang=EN-US>i</span>节点</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>指针<span lang=EN-US>inode</span>。如果当前处理的目录项是一个符号链接名，则使用<span
lang=EN-US>follow_link()</span>得到其指向的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录项名的<span lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>351</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inr = de-&gt;inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>352</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>353</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(inode = <u><span
style='color:blue'>iget</span></u>(base-&gt;i_dev,inr))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>354</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(base);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>355</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <u><span style='color:blue'>NULL</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>356</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>357</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (follow_links)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>358</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode = <u><span style='color:blue'>follow_link</span></u>(base,inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>359</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>360</span></u><span
lang=EN-US> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u><span
style='color:blue'>iput</span></u>(base);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>361</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_atime=<u><span
style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>362</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt=1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>363</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>364</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>365</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>取指定路径名的<span
lang=EN-US>i</span>节点，不跟随符号链接。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>pathname
- </span>路径名。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：对应的<span
lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>366</span></u><span
lang=EN-US> struct <u><span style='color:blue'>m_inode</span></u> * <u><span
style='color:blue'>lnamei</span></u>(const char * pathname)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>367</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>368</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return <u><span
style='color:blue'>_namei</span></u>(pathname, <u><span style='color:blue'>NULL</span></u>,
0);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>369</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>370</span></u><span
lang=EN-US> </span></p>

<p class=a><u><span lang=EN-US style='color:blue'>371</span></u><span
lang=EN-US> <b><i>/*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>372</span></u><span
lang=EN-US> <b><i>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; namei()</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>373</span></u><span
lang=EN-US> <b><i>&nbsp;*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>374</span></u><span
lang=EN-US> <b><i>&nbsp;* is used by most simple commands to get the inode of a
specified name.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>375</span></u><span
lang=EN-US> <b><i>&nbsp;* Open, link etc use their own routines, but this is
enough for things</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>376</span></u><span
lang=EN-US> <b><i>&nbsp;* like 'chmod' etc.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>377</span></u><span
lang=EN-US> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp; namei()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>该函数被许多简单命令用于取得指定路径名称的<span
lang=EN-US>i</span>节点。<span lang=EN-US>open</span>、<span lang=EN-US>link</span>等则使用它们</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>自己的相应函数。但对于象修改模式<span
lang=EN-US>'chmod'</span>等这样的命令，该函数已足够用了。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>取指定路径名的<span
lang=EN-US>i</span>节点，跟随符号链接。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>pathname
- </span>路径名。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：对应的<span
lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>378</span></u><span
lang=EN-US> struct <u><span style='color:blue'>m_inode</span></u> * <u><span
style='color:blue'>namei</span></u>(const char * pathname)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>379</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>380</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return <u><span
style='color:blue'>_namei</span></u>(pathname,<u><span style='color:blue'>NULL</span></u>,1);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>381</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>382</span></u><span
lang=EN-US> </span></p>

<p class=a><u><span lang=EN-US style='color:blue'>383</span></u><span
lang=EN-US> <b><i>/*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>384</span></u><span
lang=EN-US> <b><i>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; open_namei()</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>385</span></u><span
lang=EN-US> <b><i>&nbsp;*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>386</span></u><span
lang=EN-US> <b><i>&nbsp;* namei for open - this is in fact almost the whole
open-routine.</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>387</span></u><span
lang=EN-US> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * &nbsp;&nbsp;&nbsp;&nbsp;open_namei()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * open()</span>函数使用的<span
lang=EN-US>namei</span>函数<span lang=EN-US> - </span>这其实几乎是完整的打开文件程序。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>文件打开<span
lang=EN-US>namei</span>函数。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数<span lang=EN-US>filename</span>是文件路径名，<span
lang=EN-US>flag</span>是打开文件标志，可取值<span lang=EN-US>O_RDONLY</span>（只读）、<span
lang=EN-US>O_WRONLY</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>（只写）或<span lang=EN-US>O_RDWR</span>（读写），以及<span
lang=EN-US>O_CREAT</span>（创建）、<span lang=EN-US>O_EXCL</span>（被创建文件必须不存在）、</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // O_APPEND</span>（在文件尾添加数据）等其他一些标志的组合。如果本调用创建了一个新文件，则</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // mode</span>就用于指定文件的许可属性。这些属性有<span
lang=EN-US>S_IRWXU</span>（文件宿主具有读、写和执行权限）、</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // S_IRUSR</span>（用户具有读文件权限）、<span
lang=EN-US>S_IRWXG</span>（组成员具有读、写和执行权限）等等。对于新</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>创建的文件，这些属性只应用于将来对文件的访问，创建了只读文件的打开调用也将返回一</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>个可读写的文件句柄。参见包含文件<span
lang=EN-US>sys/stat.h</span>、<span lang=EN-US>fcntl.h</span>。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：成功返回<span
lang=EN-US>0</span>，否则返回出错码；<span lang=EN-US>res_inode - </span>返回对应文件路径名的<span
lang=EN-US>i</span>节点指针。</p>

<p class=a><u><span lang=EN-US style='color:blue'>388</span></u><span
lang=EN-US> int <u><span style='color:blue'>open_namei</span></u>(const char *
pathname, int flag, int mode,</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>389</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> ** res_inode)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>390</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>391</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>392</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int
inr,dev,namelen;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>393</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir, *inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>394</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>395</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>396</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先对函数参数进行合理的处理。如果文件访问模式标志是只读（<span
lang=EN-US>0</span>），但是文件截零标志</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // O_TRUNC</span>却置位了，则在文件打开标志中添加只写标志<span
lang=EN-US>O_WRONLY</span>。这样做的原因是由于截零</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>标志<span lang=EN-US>O_TRUNC</span>必须在文件可写情况下才有效。然后使用当前进程的文件访问许可屏蔽码，屏</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>蔽掉给定模式中的相应位，并添上普通文件标志<span
lang=EN-US>I_REGULAR</span>。该标志将用于打开的文件不存</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>在而需要创建文件时，作为新文件的默认属性。参见下面<span
lang=EN-US>411</span>行上的注释。</p>

<p class=a><u><span lang=EN-US style='color:blue'>397</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((flag &amp; <u><span
style='color:blue'>O_TRUNC</span></u>) &amp;&amp; !(flag &amp; <u><span
style='color:blue'>O_ACCMODE</span></u>))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>398</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
flag |= <u><span style='color:blue'>O_WRONLY</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>399</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mode &amp;= 0777
&amp; ~current-&gt;<u><span style='color:blue'>umask</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>400</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mode |= <u><span
style='color:blue'>I_REGULAR</span></u>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>常规文件标志。见参见<span lang=EN-US>include/const.h</span>文件）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后根据指定的路径名寻找到对应的<span
lang=EN-US>i</span>节点，以及最顶端目录名及其长度。此时如果最顶端</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录名长度为<span
lang=EN-US>0</span>（ 例如<span lang=EN-US>'</span><span lang=EN-US>/usr/' </span>这种路径名的情况），那么若操作不是读写、创建和文件长</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>度截<span lang=EN-US>0</span>，则表示是在打开一个目录名文件操作。于是直接返回该目录的<span
lang=EN-US>i</span>节点并返回<span lang=EN-US>0</span>退出。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>否则说明进程操作非法，于是放回该<span
lang=EN-US>i</span>节点，返回出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>401</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir = <u><span
style='color:blue'>dir_namei</span></u>(pathname,&amp;namelen,&amp;basename,<u><span
style='color:blue'>NULL</span></u>)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>402</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>403</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen)
{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>/* special case: '/usr/' etc */</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>404</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(flag &amp; (<u><span style='color:blue'>O_ACCMODE</span></u>|<u><span
style='color:blue'>O_CREAT</span></u>|<u><span style='color:blue'>O_TRUNC</span></u>)))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>405</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
*res_inode=dir;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>406</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>407</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>408</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>409</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EISDIR</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>410</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>接着根据上面得到的最顶层目录名的<span
lang=EN-US>i</span>节点<span lang=EN-US>dir</span>，在其中查找取得路径名字符串中最后的文</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>件名对应的目录项结构<span
lang=EN-US>de</span>，并同时得到该目录项所在的高速缓冲区指针。 如果该高速缓冲</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>指针为<span lang=EN-US>NULL</span>，则表示没有找到对应文件名的目录项，因此只可能是创建文件操作。
此时如</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>果不是创建文件，则放回该目录的<span
lang=EN-US>i</span>节点，返回出错号退出。如果用户在该目录没有写的权</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>力，则放回该目录的<span
lang=EN-US>i</span>节点，返回出错号退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>411</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>412</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>413</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(flag &amp; <u><span style='color:blue'>O_CREAT</span></u>)) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>414</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>415</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>416</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>417</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!<u><span style='color:blue'>permission</span></u>(dir,<u><span
style='color:blue'>MAY_WRITE</span></u>)) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>418</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>419</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EACCES</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>420</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们确定了是创建操作并且有写操作许可。 因此我们就在目录<span
lang=EN-US>i</span>节点对应设备上申请</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>一个新的<span lang=EN-US>i</span>节点给路径名上指定的文件使用。
若失败则放回目录的<span lang=EN-US>i</span>节点，并返回没有空</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>间出错码。否则使用该新 <span
lang=EN-US>i</span>节点，对其进行初始设置：置节点的用户<span lang=EN-US>id</span>；对应节点访问模</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>式；置已修改标志。然后并在指定目录<span
lang=EN-US>dir</span>中添加一个新目录项。</p>

<p class=a><u><span lang=EN-US style='color:blue'>421</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode = <u><span style='color:blue'>new_inode</span></u>(dir-&gt;i_dev);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>422</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!inode) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>423</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>424</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>425</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>426</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_uid = <u><span style='color:blue'>current</span></u>-&gt;euid;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>427</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_mode = mode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>428</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>429</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bh = <u><span style='color:blue'>add_entry</span></u>(dir,basename,namelen,&amp;de);</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果返回的应该含有新目录项的高速缓冲区指针为<span
lang=EN-US>NULL</span>，则表示添加目录项操作失败。于是</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>将该新<span lang=EN-US>i</span>节点的引用连接计数减<span
lang=EN-US>1</span>，放回该<span lang=EN-US>i</span>节点与目录的<span lang=EN-US>i</span>节点并返回出错码退出。
否则</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>说明添加目录项操作成功。 于是我们来设置该新目录项的一些初始值：置<span
lang=EN-US>i</span>节点号为新申请</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>到的<span lang=EN-US>i</span>节点的号码；并置高速缓冲区已修改标志。
然后释放该高速缓冲区，放回目录的<span lang=EN-US>i</span>节</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>点。返回新目录项的<span
lang=EN-US>i</span>节点指针，并成功退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>430</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>431</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>432</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>433</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>434</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>435</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>436</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de-&gt;inode = inode-&gt;i_num;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>437</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bh-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>438</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>439</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>440</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
*res_inode = inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>441</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>442</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>若上面（<span lang=EN-US>411</span>行）在目录中取文件名对应目录项结构的操作成功（即<span
lang=EN-US>bh</span>不为<span lang=EN-US>NULL</span>），则说</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>明指定打开的文件已经存在。于是取出该目录项的<span
lang=EN-US>i</span>节点号和其所在设备号，并释放该高速</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>缓冲区以及放回目录的<span
lang=EN-US>i</span>节点。如果此时独占操作标志<span lang=EN-US>O_EXCL</span>置位，但现在文件已经存在，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则返回文件已存在出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>443</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inr = de-&gt;inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>444</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dev =
dir-&gt;i_dev;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>445</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>446</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (flag &amp; <u><span
style='color:blue'>O_EXCL</span></u>) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>447</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>448</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EEXIST</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>449</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后我们读取该目录项的<span
lang=EN-US>i</span>节点内容。若该<span lang=EN-US>i</span>节点是一个目录的<span lang=EN-US>i</span>节点并且访问模式是只</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>写或读写，或者没有访问的许可权限，则放回该<span
lang=EN-US>i</span>节点，返回访问权限出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>450</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(inode = <u><span
style='color:blue'>follow_link</span></u>(dir,<u><span style='color:blue'>iget</span></u>(dev,inr))))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>451</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EACCES</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>452</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((<u><span
style='color:blue'>S_ISDIR</span></u>(inode-&gt;i_mode) &amp;&amp; (flag &amp; <u><span
style='color:blue'>O_ACCMODE</span></u>)) ||</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>453</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
!<u><span style='color:blue'>permission</span></u>(inode,<u><span
style='color:blue'>ACC_MODE</span></u>(flag))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>454</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>455</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>456</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>接着我们更新该<span
lang=EN-US>i</span>节点的访问时间字段值为当前时间。如果设立了截<span lang=EN-US>0</span>标志，则将该<span
lang=EN-US>i</span>节</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>点的文件长度截为<span
lang=EN-US>0</span>。最后返回该目录项<span lang=EN-US>i</span>节点的指针，并返回<span lang=EN-US>0</span>（成功）。</p>

<p class=a><u><span lang=EN-US style='color:blue'>457</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_atime =
<u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>458</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (flag &amp; <u><span
style='color:blue'>O_TRUNC</span></u>)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>459</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>truncate</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>460</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *res_inode = inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>461</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>462</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>463</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>创建一个设备特殊文件或普通文件节点（<span
lang=EN-US>node</span>）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp; &nbsp;// </span>该函数创建名称为<span
lang=EN-US>filename</span>，由<span lang=EN-US>mode</span>和<span lang=EN-US>dev</span>指定的文件系统节点（普通文件、设备特殊文</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>件或命名管道）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>filename
- </span>路径名；<span lang=EN-US>mode - </span>指定使用许可以及所创建节点的类型；<span lang=EN-US>dev
- </span>设备号。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：成功则返回<span
lang=EN-US>0</span>，否则返回出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>464</span></u><span
lang=EN-US> int <u><span style='color:blue'>sys_mknod</span></u>(const char *
filename, int mode, int dev)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>465</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>466</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>467</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int namelen;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>468</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir, * inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>469</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>470</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>471</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先检查操作许可和参数的有效性并取路径名中顶层目录的<span
lang=EN-US>i</span>节点。如果不是超级用户，则</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回访问许可出错码。如果找不到对应路径名中顶层目录的<span
lang=EN-US>i</span>节点，则返回出错码。如果最</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>顶端的文件名长度为<span
lang=EN-US>0</span>，则说明给出的路径名最后没有指定文件名，放回该目录<span lang=EN-US>i</span>节点，返</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>回出错码退出。如果在该目录中没有写的权限，则放回该目录的<span
lang=EN-US>i</span>节点，返回访问许可出错</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>码退出。如果不是超级用户，则返回访问许可出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>472</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>suser</span></u>())</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>473</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>474</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir = <u><span
style='color:blue'>dir_namei</span></u>(filename,&amp;namelen,&amp;basename, <u><span
style='color:blue'>NULL</span></u>)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>475</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>476</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>477</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>478</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>479</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>480</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>permission</span></u>(dir,<u><span style='color:blue'>MAY_WRITE</span></u>))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>481</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>482</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>483</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后我们搜索一下路径名指定的文件是否已经存在。若已经存在则不能创建同名文件节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果对应路径名上最后的文件名的目录项已经存在，则释放包含该目录项的缓冲区块并放回</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的<span lang=EN-US>i</span>节点，返回文件已经存在的出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>484</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>485</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>486</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>487</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>488</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EEXIST</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>489</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>否则我们就申请一个新的<span
lang=EN-US>i</span>节点，并设置该<span lang=EN-US>i</span>节点的属性模式。如果要创建的是块设备文件</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>或者是字符设备文件，则令<span
lang=EN-US>i</span>节点的直接逻辑块指针<span lang=EN-US>0</span>等于设备号。即对于设备文件来说，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>其<span lang=EN-US>i</span>节点的<span
lang=EN-US>i_zone[0]</span>中存放的是该设备文件所定义设备的设备号。然后设置该<span lang=EN-US>i</span>节点的修</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>改时间、访问时间为当前时间，并设置<span
lang=EN-US>i</span>节点已修改标志。</p>

<p class=a><u><span lang=EN-US style='color:blue'>490</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode = <u><span
style='color:blue'>new_inode</span></u>(dir-&gt;i_dev);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>491</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!inode) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>若不成功则放回目录<span lang=EN-US>i</span>节点，返回无空间出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>492</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>493</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>494</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>495</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_mode =
mode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>496</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (<u><span
style='color:blue'>S_ISBLK</span></u>(mode) || <u><span style='color:blue'>S_ISCHR</span></u>(mode))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>497</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_zone[0] = dev;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>498</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_mtime =
inode-&gt;i_atime = <u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>499</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>接着为这个新的<span
lang=EN-US>i</span>节点在目录中新添加一个目录项。如果失败（包含该目录项的高速缓冲</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>块指针为<span lang=EN-US>NULL</span>），则放回目录的<span
lang=EN-US>i</span>节点；把所申请的<span lang=EN-US>i</span>节点引用连接计数复位，并放回该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // i</span>节点，返回出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>500</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>add_entry</span></u>(dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>501</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>502</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>503</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks=0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>504</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>505</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>506</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在添加目录项操作也成功了，于是我们来设置这个目录项内容。令该目录项的<span
lang=EN-US>i</span>节点字</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>段等于新<span lang=EN-US>i</span>节点号，并置高速缓冲区已修改标志，放回目录和新的<span
lang=EN-US>i</span>节点，释放高速缓冲</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>区，最后返回<span
lang=EN-US>0(</span>成功<span lang=EN-US>)</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>507</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-&gt;inode =
inode-&gt;i_num;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>508</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>509</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>510</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>511</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>512</span></u><span
lang=EN-US> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>513</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>514</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; ////&nbsp; </span>创建一个目录。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>pathname
- </span>路径名；<span lang=EN-US>mode - </span>目录使用的权限属性。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：成功则返回<span
lang=EN-US>0</span>，否则返回出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>515</span></u><span
lang=EN-US> int <u><span style='color:blue'>sys_mkdir</span></u>(const char *
pathname, int mode)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>516</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>517</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>518</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int namelen;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>519</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir, * inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>520</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh, *dir_block;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>521</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>522</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先检查参数的有效性并取路径名中顶层目录的<span
lang=EN-US>i</span>节点。如果找不到对应路径名中顶层目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的<span lang=EN-US>i</span>节点，则返回出错码。如果最顶端的文件名长度为<span
lang=EN-US>0</span>，则说明给出的路径名最后没有指</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>定文件名，放回该目录<span
lang=EN-US>i</span>节点，返回出错码退出。如果在该目录中没有写的权限，则放回该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的<span lang=EN-US>i</span>节点，返回访问许可出错码退出。如果不是超级用户，则返回访问许可出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>523</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir = <u><span
style='color:blue'>dir_namei</span></u>(pathname,&amp;namelen,&amp;basename, <u><span
style='color:blue'>NULL</span></u>)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>524</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>525</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>526</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>527</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>528</span></u><span
lang=EN-US>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>529</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>permission</span></u>(dir,<u><span style='color:blue'>MAY_WRITE</span></u>))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>530</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>531</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>532</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后我们搜索一下路径名指定的目录名是否已经存在。若已经存在则不能创建同名目录节点。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果对应路径名上最后的目录名的目录项已经存在，则释放包含该目录项的缓冲区块并放回</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的<span lang=EN-US>i</span>节点，返回文件已经存在的出错码退出。否则我们就申请一个新的<span
lang=EN-US>i</span>节点，并设置</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>该<span lang=EN-US>i</span>节点的属性模式：置该新<span
lang=EN-US>i</span>节点对应的文件长度为<span lang=EN-US>32</span>字节 （<span lang=EN-US>2</span>个目录项的大小）、置</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>节点已修改标志，以及节点的修改时间和访问时间。<span
lang=EN-US>2</span>个目录项分别用于<span lang=EN-US>'.'</span>和<span lang=EN-US>'..'</span>目录。</p>

<p class=a><u><span lang=EN-US style='color:blue'>533</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>534</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>535</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>536</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>537</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EEXIST</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>538</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>539</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode = <u><span
style='color:blue'>new_inode</span></u>(dir-&gt;i_dev);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>540</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!inode) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>若不成功则放回目录的<span lang=EN-US>i</span>节点，返回无空间出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>541</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>542</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>543</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>544</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_size =
32;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>545</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>546</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_mtime =
inode-&gt;i_atime = <u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>接着为该新<span lang=EN-US>i</span>节点申请一用于保存目录项数据的磁盘块，并令<span
lang=EN-US>i</span>节点的第一个直接块指针等</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>于该块号。如果申请失败则放回对应目录的<span
lang=EN-US>i</span>节点；复位新申请的<span lang=EN-US>i</span>节点连接计数；放回该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>新的<span lang=EN-US>i</span>节点，返回没有空间出错码退出。否则置该新的<span
lang=EN-US>i</span>节点已修改标志。</p>

<p class=a><u><span lang=EN-US style='color:blue'>547</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(!(inode-&gt;i_zone[0]=<u><span style='color:blue'>new_block</span></u>(inode-&gt;i_dev)))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>548</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>549</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>550</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>551</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>552</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>553</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>从设备上读取新申请的磁盘块（目的是把对应块放到高速缓冲区中）。若出错，则放回对应</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的<span lang=EN-US>i</span>节点；释放申请的磁盘块；复位新申请的<span
lang=EN-US>i</span>节点连接计数；放回该新的<span lang=EN-US>i</span>节点，返</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>回没有空间出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>554</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir_block=<u><span
style='color:blue'>bread</span></u>(inode-&gt;i_dev,inode-&gt;i_zone[0]))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>555</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>556</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>557</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>558</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ERROR</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>559</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后我们在缓冲块中建立起所创建目录文件中的<span
lang=EN-US>2</span>个默认的新目录项（<span lang=EN-US>'.'</span>和<span lang=EN-US>'..'</span>）结构数</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>据。首先令<span lang=EN-US>de</span>指向存放目录项的数据块，然后置该目录项的<span
lang=EN-US>i</span>节点号字段等于新申请的<span lang=EN-US>i</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>节点号，名字字段等于<span
lang=EN-US>&quot;.&quot;</span>。 然后<span lang=EN-US>de</span>指向下一个目录项结构，并在该结构中存放上级目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的 <span lang=EN-US>i</span>节点号和名字<span
lang=EN-US>&quot;..&quot;</span>。然后设置该高速缓冲块已修改标志，并释放该缓冲块。再初始化</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>设置新<span lang=EN-US>i</span>节点的模式字段，并置该<span
lang=EN-US>i</span>节点已修改标志。</p>

<p class=a><u><span lang=EN-US style='color:blue'>560</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de = (struct <u><span
style='color:blue'>dir_entry</span></u> *) dir_block-&gt;b_data;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>561</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de-&gt;inode=inode-&gt;i_num;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//
</span>设置<span lang=EN-US>'.'</span>目录项。</p>

<p class=a><u><span lang=EN-US style='color:blue'>562</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>strcpy</span></u>(de-&gt;name,<i>&quot;.&quot;</i>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>563</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>564</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-&gt;inode =
dir-&gt;i_num;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// </span>设置<span lang=EN-US>'..'</span>目录项。</p>

<p class=a><u><span lang=EN-US style='color:blue'>565</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>strcpy</span></u>(de-&gt;name,<i>&quot;..&quot;</i>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>566</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_nlinks
= 2;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>567</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dir_block-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>568</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(dir_block);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>569</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_mode = <u><span
style='color:blue'>I_DIRECTORY</span></u> | (mode &amp; 0777 &amp;
~current-&gt;<u><span style='color:blue'>umask</span></u>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>570</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们在指定目录中新添加一个目录项，用于存放新建目录的<span
lang=EN-US>i</span>节点和目录名。如果失</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>败（包含该目录项的高速缓冲区指针为<span
lang=EN-US>NULL</span>），则放回目录的<span lang=EN-US>i</span>节点；所申请的<span lang=EN-US>i</span>节点引</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>用连接计数复位，并放回该<span
lang=EN-US>i</span>节点。返回出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>571</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>add_entry</span></u>(dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>572</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>573</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>574</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks=0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>575</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>576</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>577</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>最后令该新目录项的<span
lang=EN-US>i</span>节点字段等于新<span lang=EN-US>i</span>节点号，并置高速缓冲块已修改标志，放回目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>和新的<span lang=EN-US>i</span>节点，释放高速缓冲区，最后返回<span
lang=EN-US>0</span>（成功）。</p>

<p class=a><u><span lang=EN-US style='color:blue'>578</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-&gt;inode =
inode-&gt;i_num;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>579</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>580</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir-&gt;i_nlinks++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>581</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir-&gt;i_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>582</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>583</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>584</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>585</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>586</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>587</span></u><span
lang=EN-US> </span></p>

<p class=a><u><span lang=EN-US style='color:blue'>588</span></u><span
lang=EN-US> <b><i>/*</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>589</span></u><span
lang=EN-US> <b><i>&nbsp;* routine to check that the specified directory is
empty (for rmdir)</i></b></span></p>

<p class=a><u><span lang=EN-US style='color:blue'>590</span></u><span
lang=EN-US> <b><i>&nbsp;*/</i></b></span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; /*</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; * </span>用于检查指定的目录是否为空的子程序（用于<span
lang=EN-US>rmdir</span>系统调用）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>检查指定目录是否空。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>inode
- </span>指定目录的<span lang=EN-US>i</span>节点指针。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：<span lang=EN-US>1 –
</span>目录中是空的；<span lang=EN-US>0 - </span>不空。</p>

<p class=a><u><span lang=EN-US style='color:blue'>591</span></u><span
lang=EN-US> static int <u><span style='color:blue'>empty_dir</span></u>(struct <u><span
style='color:blue'>m_inode</span></u> * inode)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>592</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>593</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int nr,block;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>594</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;int len;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>595</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>596</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>597</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先计算指定目录中现有目录项个数并检查开始两个特定目录项中信息是否正确。一个目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>中应该起码有<span
lang=EN-US>2</span>个目录项：即<span lang=EN-US>&quot;.&quot;</span>和<span
lang=EN-US>&quot;..&quot;</span>。 如果目录项个数少于<span lang=EN-US>2</span>个或者该目录<span
lang=EN-US>i</span>节点的第</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // 1</span>个直接块没有指向任何磁盘块号，或者该直接块读不出，则显示警告信息“设备<span
lang=EN-US>dev </span>上目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>录错”，返回<span
lang=EN-US>0(</span>失败<span lang=EN-US>)</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>598</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len =
inode-&gt;i_size / sizeof (struct <u><span style='color:blue'>dir_entry</span></u>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// </span>目录中目录项个数。</p>

<p class=a><u><span lang=EN-US style='color:blue'>599</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (len&lt;2 ||
!inode-&gt;i_zone[0] ||</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>600</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
!(bh=<u><span style='color:blue'>bread</span></u>(inode-&gt;i_dev,inode-&gt;i_zone[0])))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>601</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>printk</span></u>(<i>&quot;warning - bad directory
on dev %04x\n&quot;</i>,inode-&gt;i_dev);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>602</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>603</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>此时<span lang=EN-US>bh</span>所指缓冲块中含有目录项数据。我们让目录项指针<span
lang=EN-US>de</span>指向缓冲块中第<span lang=EN-US>1</span>个目录项。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>对于第<span lang=EN-US>1</span>个目录项（<span
lang=EN-US>&quot;.&quot;</span>），它的<span lang=EN-US>i</span>节点号字段<span
lang=EN-US>inode</span>应该等于当前目录的<span lang=EN-US>i</span>节点号。对于</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>第<span lang=EN-US>2</span>个目录项（<span
lang=EN-US>&quot;..&quot;</span>），它的<span lang=EN-US>i</span>节点号字段<span
lang=EN-US> inode </span>应该等于上一层目录的<span lang=EN-US>i</span>节点号，不会</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>为<span lang=EN-US>0</span>。因此如果第<span
lang=EN-US>1</span>个目录项的<span lang=EN-US>i</span>节点号字段值不等于该目录的<span lang=EN-US>i</span>节点号，或者第<span
lang=EN-US>2</span>个目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>项的<span lang=EN-US>i</span>节点号字段为零，或者两个目录项的名字字段不分别等于<span
lang=EN-US>&quot;.&quot;</span>和<span lang=EN-US>&quot;..&quot;</span>，则显示出错警</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>告信息“设备<span
lang=EN-US>dev</span>上目录错”，并返回<span lang=EN-US>0</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>604</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de = (struct <u><span
style='color:blue'>dir_entry</span></u> *) bh-&gt;b_data;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>605</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (de[0].inode !=
inode-&gt;i_num || !de[1].inode || </span></p>

<p class=a><u><span lang=EN-US style='color:blue'>606</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>strcmp</span></u>(<i>&quot;.&quot;</i>,de[0].name)
|| <u><span style='color:blue'>strcmp</span></u>(<i>&quot;..&quot;</i>,de[1].name))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>607</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>printk</span></u>(<i>&quot;warning - bad directory
on dev %04x\n&quot;</i>,inode-&gt;i_dev);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>608</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>609</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后我们令<span lang=EN-US>nr</span>等于目录项序号（从<span
lang=EN-US>0</span>开始计）；<span lang=EN-US>de</span>指向第三个目录项。并循环检测该目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>中其余所有的（<span
lang=EN-US>len - 2</span>）个目录项，看有没有目录项的<span lang=EN-US>i</span>节点号字段不为<span
lang=EN-US>0</span>（被使用）。</p>

<p class=a><u><span lang=EN-US style='color:blue'>610</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nr = 2;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>611</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de += 2;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>612</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (nr&lt;len) {</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果该块磁盘块中的目录项已经全部检测完毕，则释放该磁盘块的缓冲块，并读取目录数据</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>文件中下一块含有目录项的磁盘块。读取的方法是根据当前检测的目录项序号<span
lang=EN-US> nr </span>计算出对</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>应目录项在目录数据文件中的数据块号（<span
lang=EN-US>nr/DIR_ENTRIES_PER_BLOCK</span>），然后使用<span lang=EN-US> bmap()</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>函数取得对应的盘块号<span
lang=EN-US> block</span>，再使用读设备盘块函数<span lang=EN-US>bread() </span>把相应盘块读入缓冲块中，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>并返回该缓冲块的指针。若所读取的相应盘块没有使用（或已经不用，如文件已经删除等），</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则继续读下一块，若读不出，则出错返回<span
lang=EN-US>0</span>。否则让<span lang=EN-US>de</span>指向读出块的首个目录项。</p>

<p class=a><u><span lang=EN-US style='color:blue'>613</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if ((void *) de &gt;= (void *) (bh-&gt;b_data+<u><span style='color:blue'>BLOCK_SIZE</span></u>))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>614</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>615</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block=<u><span
style='color:blue'>bmap</span></u>(inode,nr/<u><span style='color:blue'>DIR_ENTRIES_PER_BLOCK</span></u>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>616</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!block) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>617</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
nr += <u><span style='color:blue'>DIR_ENTRIES_PER_BLOCK</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>618</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
continue;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>619</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>620</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (!(bh=<u><span style='color:blue'>bread</span></u>(inode-&gt;i_dev,block)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>621</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>622</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de = (struct <u><span style='color:blue'>dir_entry</span></u> *) bh-&gt;b_data;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>623</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>对于<span lang=EN-US>de</span>指向的当前目录项，如果该目录项的<span
lang=EN-US>i</span>节点号字段不等于<span lang=EN-US>0</span>，则表示该目录项目前正</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>被使用，则释放该高速缓冲区，返回<span
lang=EN-US>0</span>退出。否则，若还没有查询完该目录中的所有目录项，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则把目录项序号<span
lang=EN-US>nr</span>增<span lang=EN-US>1</span>、<span lang=EN-US>de</span>指向下一个目录项，继续检测。</p>

<p class=a><u><span lang=EN-US style='color:blue'>624</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (de-&gt;inode) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>625</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>626</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>627</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>628</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
de++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>629</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;nr++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>630</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>执行到这里说明该目录中没有找到已用的目录项<span
lang=EN-US>(</span>当然除了头两个以外<span lang=EN-US>)</span>，则释放缓冲块返回<span lang=EN-US>1</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>631</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L632><u><span lang=EN-US style='color:blue'>632</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;</span></p>

<p class=a><a name=L633><u><span lang=EN-US style='color:blue'>633</span></u></a><span
lang=EN-US> }</span></p>

<p class=a><a name=L634><u><span lang=EN-US style='color:blue'>634</span></u></a><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>删除目录。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数： <span lang=EN-US>name
- </span>目录名（路径名）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：返回<span lang=EN-US>0</span>表示成功，否则返回出错号。</p>

<p class=a><a name=L635><u><span lang=EN-US style='color:blue'>635</span></u></a><span
lang=EN-US> int <u><span style='color:blue'>sys_rmdir</span></u>(const char *
name)</span></p>

<p class=a><a name=L636><u><span lang=EN-US style='color:blue'>636</span></u></a><span
lang=EN-US> {</span></p>

<p class=a><a name=L637><u><span lang=EN-US style='color:blue'>637</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><a name=L638><u><span lang=EN-US style='color:blue'>638</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int namelen;</span></p>

<p class=a><a name=L639><u><span lang=EN-US style='color:blue'>639</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir, * inode;</span></p>

<p class=a><a name=L640><u><span lang=EN-US style='color:blue'>640</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><a name=L641><u><span lang=EN-US style='color:blue'>641</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><a name=L642><u><span lang=EN-US style='color:blue'>642</span></u></a><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先检查参数的有效性并取路径名中顶层目录的<span
lang=EN-US>i</span>节点。如果找不到对应路径名中顶层目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的<span lang=EN-US>i</span>节点，则返回出错码。如果最顶端的文件名长度为<span
lang=EN-US>0</span>，则说明给出的路径名最后没有指</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>定文件名，放回该目录<span
lang=EN-US>i</span>节点，返回出错码退出。如果在该目录中没有写的权限，则放回该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的<span lang=EN-US>i</span>节点，返回访问许可出错码退出。如果不是超级用户，则返回访问许可出错码。</p>

<p class=a><a name=L643><u><span lang=EN-US style='color:blue'>643</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir = <u><span
style='color:blue'>dir_namei</span></u>(name,&amp;namelen,&amp;basename, <u><span
style='color:blue'>NULL</span></u>)))</span></p>

<p class=a><a name=L644><u><span lang=EN-US style='color:blue'>644</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><a name=L645><u><span lang=EN-US style='color:blue'>645</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen) {</span></p>

<p class=a><a name=L646><u><span lang=EN-US style='color:blue'>646</span></u></a><span
lang=EN-US> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L647><u><span lang=EN-US style='color:blue'>647</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><a name=L648><u><span lang=EN-US style='color:blue'>648</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><a name=L649><u><span lang=EN-US style='color:blue'>649</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>permission</span></u>(dir,<u><span style='color:blue'>MAY_WRITE</span></u>))
{</span></p>

<p class=a><a name=L650><u><span lang=EN-US style='color:blue'>650</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L651><u><span lang=EN-US style='color:blue'>651</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><a name=L652><u><span lang=EN-US style='color:blue'>652</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后根据指定目录的<span
lang=EN-US>i</span>节点和目录名利用函数<span lang=EN-US>find_entry()</span>寻找对应目录项，并返回包含该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录项的缓冲块指针<span
lang=EN-US>bh</span>、包含该目录项的目录的<span lang=EN-US>i</span>节点指针<span lang=EN-US>dir</span>和该目录项指针<span
lang=EN-US>de</span>。再根据</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>该目录项<span lang=EN-US>de
</span>中的<span lang=EN-US>i</span>节点号利用<span lang=EN-US> iget()</span>函数得到对应的<span
lang=EN-US>i</span>节点<span lang=EN-US> inode</span>。如果对应路径名上最</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>后目录名的目录项不存在，则释放包含该目录项的高速缓冲区，放回目录的
<span lang=EN-US>i</span>节点，返回文</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>件已经存在出错码，并退出。如果取目录项的<span
lang=EN-US>i</span>节点出错，则放回目录的 <span lang=EN-US>i</span>节点，并释放含</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>有目录项的高速缓冲区，返回出错号。</p>

<p class=a><a name=L653><u><span lang=EN-US style='color:blue'>653</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;dir,basename,namelen,&amp;de);</span></p>

<p class=a><a name=L654><u><span lang=EN-US style='color:blue'>654</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><a name=L655><u><span lang=EN-US style='color:blue'>655</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L656><u><span lang=EN-US style='color:blue'>656</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><a name=L657><u><span lang=EN-US style='color:blue'>657</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><a name=L658><u><span lang=EN-US style='color:blue'>658</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(inode = <u><span
style='color:blue'>iget</span></u>(dir-&gt;i_dev, de-&gt;inode))) {</span></p>

<p class=a><a name=L659><u><span lang=EN-US style='color:blue'>659</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L660><u><span lang=EN-US style='color:blue'>660</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L661><u><span lang=EN-US style='color:blue'>661</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><a name=L662><u><span lang=EN-US style='color:blue'>662</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>此时我们已有包含要被删除目录项的目录<span
lang=EN-US>i</span>节点<span lang=EN-US>dir</span>、要被删除目录项的<span lang=EN-US>i</span>节点<span
lang=EN-US>inode</span>和要</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>被删除目录项指针<span
lang=EN-US>de</span>。下面我们通过对这<span lang=EN-US>3</span>个对象中信息的检查来验证删除操作的可行性。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>若该目录设置了受限删除标志并且进程的有效用户<span
lang=EN-US>id</span>（<span lang=EN-US>euid</span>）不是<span lang=EN-US>root</span>，并且进程的有效</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>用户<span lang=EN-US>id</span>（<span
lang=EN-US>euid</span>）不等于该<span lang=EN-US>i</span>节点的用户<span lang=EN-US>id</span>，则表示当前进程没有权限删除该目录，于是放</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>回包含要删除目录名的目录 <span
lang=EN-US>i</span>节点和该要删除目录的 <span lang=EN-US>i</span>节点，然后释放高速缓冲区，返回</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>出错码。</p>

<p class=a><a name=L663><u><span lang=EN-US style='color:blue'>663</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((dir-&gt;i_mode
&amp; <u><span style='color:blue'>S_ISVTX</span></u>) &amp;&amp; <u><span
style='color:blue'>current</span></u>-&gt;euid &amp;&amp;</span></p>

<p class=a><a name=L664><u><span lang=EN-US style='color:blue'>664</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_uid != <u><span style='color:blue'>current</span></u>-&gt;euid) {</span></p>

<p class=a><a name=L665><u><span lang=EN-US style='color:blue'>665</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L666><u><span lang=EN-US style='color:blue'>666</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><a name=L667><u><span lang=EN-US style='color:blue'>667</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L668><u><span lang=EN-US style='color:blue'>668</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><a name=L669><u><span lang=EN-US style='color:blue'>669</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果要被删除的目录项<span
lang=EN-US>i</span>节点的设备号不等于包含该目录项的目录的设备号，或者该被删除</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的引用连接计数大于<span
lang=EN-US> 1</span>（表示有符号连接等），则不能删除该目录。于是释放包含要删</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>除目录名的目录<span
lang=EN-US>i</span>节点和该要删除目录的<span lang=EN-US>i</span>节点，释放高速缓冲块，返回出错码。</p>

<p class=a><a name=L670><u><span lang=EN-US style='color:blue'>670</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (inode-&gt;i_dev
!= dir-&gt;i_dev || inode-&gt;i_count&gt;1) {</span></p>

<p class=a><a name=L671><u><span lang=EN-US style='color:blue'>671</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L672><u><span lang=EN-US style='color:blue'>672</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><a name=L673><u><span lang=EN-US style='color:blue'>673</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L674><u><span lang=EN-US style='color:blue'>674</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><a name=L675><u><span lang=EN-US style='color:blue'>675</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果要被删除目录的目录项<span
lang=EN-US>i</span>节点就等于包含该需删除目录的目录<span lang=EN-US>i</span>节点，则表示试图删除</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // &quot;.&quot;</span>目录，这是不允许的。于是放回包含要删除目录名的目录<span
lang=EN-US>i</span>节点和要删除目录的<span lang=EN-US>i</span>节点，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>释放高速缓冲块，返回出错码。</p>

<p class=a><a name=L676><u><span lang=EN-US style='color:blue'>676</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (inode == dir)
{&nbsp;&nbsp;&nbsp;&nbsp; <b><i>/* we may not delete &quot;.&quot;, but
&quot;../dir&quot; is ok */</i></b></span></p>

<p class=a><a name=L677><u><span lang=EN-US style='color:blue'>677</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><a name=L678><u><span lang=EN-US style='color:blue'>678</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L679><u><span lang=EN-US style='color:blue'>679</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L680><u><span lang=EN-US style='color:blue'>680</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><a name=L681><u><span lang=EN-US style='color:blue'>681</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>若要被删除目录<span
lang=EN-US>i</span>节点的属性表明这不是一个目录，则本删除操作的前提完全不存在。于是</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>放回包含删除目录名的目录<span
lang=EN-US>i</span>节点和该要删除目录的<span lang=EN-US>i</span>节点，释放高速缓冲块，返回出错码。</p>

<p class=a><a name=L682><u><span lang=EN-US style='color:blue'>682</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>S_ISDIR</span></u>(inode-&gt;i_mode)) {</span></p>

<p class=a><a name=L683><u><span lang=EN-US style='color:blue'>683</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><a name=L684><u><span lang=EN-US style='color:blue'>684</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L685><u><span lang=EN-US style='color:blue'>685</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L686><u><span lang=EN-US style='color:blue'>686</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOTDIR</span></u>;</span></p>

<p class=a><a name=L687><u><span lang=EN-US style='color:blue'>687</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>若该需被删除的目录不空，则也不能删除。于是放回包含要删除目录名的目录<span
lang=EN-US>i</span>节点和该要</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>删除目录的<span lang=EN-US>i</span>节点，释放高速缓冲块，返回出错码。</p>

<p class=a><a name=L688><u><span lang=EN-US style='color:blue'>688</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>empty_dir</span></u>(inode)) {</span></p>

<p class=a><a name=L689><u><span lang=EN-US style='color:blue'>689</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><a name=L690><u><span lang=EN-US style='color:blue'>690</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L691><u><span lang=EN-US style='color:blue'>691</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L692><u><span lang=EN-US style='color:blue'>692</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOTEMPTY</span></u>;</span></p>

<p class=a><a name=L693><u><span lang=EN-US style='color:blue'>693</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>对于一个空目录，其目录项链接数应该为<span
lang=EN-US>2</span>（链接到上层目录和本目录）。若该需被删除目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>录的<span lang=EN-US>i</span>节点的连接数不等于<span
lang=EN-US>2</span>，则显示警告信息。但删除操作仍然继续执行。于是置该需被</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>删除目录的目录项的<span
lang=EN-US>i</span>节点号字段为<span lang=EN-US>0</span>，表示该目录项不再使用，并置含有该目录项的高速</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>缓冲块已修改标志，并释放该缓冲块。然后再置被删除目录<span
lang=EN-US>i</span>节点的链接数为<span lang=EN-US>0</span>（表示空闲），</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>并置<span lang=EN-US>i</span>节点已修改标志。</p>

<p class=a><a name=L694><u><span lang=EN-US style='color:blue'>694</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(inode-&gt;i_nlinks != 2)</span></p>

<p class=a><a name=L695><u><span lang=EN-US style='color:blue'>695</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>printk</span></u>(<i>&quot;empty directory has
nlink!=2 (%d)&quot;</i>,inode-&gt;i_nlinks);</span></p>

<p class=a><a name=L696><u><span lang=EN-US style='color:blue'>696</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-&gt;inode = 0;</span></p>

<p class=a><a name=L697><u><span lang=EN-US style='color:blue'>697</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh-&gt;b_dirt = 1;</span></p>

<p class=a><a name=L698><u><span lang=EN-US style='color:blue'>698</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><a name=L699><u><span lang=EN-US style='color:blue'>699</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks=0;</span></p>

<p class=a><a name=L700><u><span lang=EN-US style='color:blue'>700</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt=1;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>再将包含被删除目录名的目录的<span
lang=EN-US>i</span>节点链接计数减<span lang=EN-US>1</span>，修改其改变时间和修改时间为当前时</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>间，并置该节点已修改标志。最后放回包含要删除目录名的目录<span
lang=EN-US>i</span>节点和该要删除目录的<span lang=EN-US>i</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>节点，返回<span lang=EN-US>0</span>（删除操作成功）。</p>

<p class=a><a name=L701><u><span lang=EN-US style='color:blue'>701</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir-&gt;i_nlinks--;</span></p>

<p class=a><a name=L702><u><span lang=EN-US style='color:blue'>702</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir-&gt;i_ctime =
dir-&gt;i_mtime = <u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><a name=L703><u><span lang=EN-US style='color:blue'>703</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir-&gt;i_dirt=1;</span></p>

<p class=a><a name=L704><u><span lang=EN-US style='color:blue'>704</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><a name=L705><u><span lang=EN-US style='color:blue'>705</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><a name=L706><u><span lang=EN-US style='color:blue'>706</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</span></p>

<p class=a><a name=L707><u><span lang=EN-US style='color:blue'>707</span></u></a><span
lang=EN-US> }</span></p>

<p class=a><a name=L708><u><span lang=EN-US style='color:blue'>708</span></u></a><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>删除（释放）文件名对应的目录项。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>从文件系统删除一个名字。如果是文件的最后一个链接，并且没有进程正打开该文件，则该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>文件也将被删除，并释放所占用的设备空间。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>name
- </span>文件名（路径名）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：成功则返回<span
lang=EN-US>0</span>，否则返回出错号。</p>

<p class=a><a name=L709><u><span lang=EN-US style='color:blue'>709</span></u></a><span
lang=EN-US> int <u><span style='color:blue'>sys_unlink</span></u>(const char *
name)</span></p>

<p class=a><a name=L710><u><span lang=EN-US style='color:blue'>710</span></u></a><span
lang=EN-US> {</span></p>

<p class=a><a name=L711><u><span lang=EN-US style='color:blue'>711</span></u></a><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>712</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int namelen;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>713</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir, * inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>714</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>715</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>716</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先检查参数的有效性并取路径名中顶层目录的<span
lang=EN-US>i</span>节点。如果找不到对应路径名中顶层目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的 <span lang=EN-US>i</span>节点，则返回出错码。如果最顶端的文件名长度为<span
lang=EN-US>0</span>，则说明给出的路径名最后没有指</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>定文件名，放回该目录<span
lang=EN-US>i</span>节点，返回出错码退出。如果在该目录中没有写的权限，则放回该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录的<span lang=EN-US>i</span>节点，返回访问许可出错码退出。如果找不到对应路径名顶层目录的<span
lang=EN-US>i</span>节点，则返</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>回出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>717</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(dir = <u><span
style='color:blue'>dir_namei</span></u>(name,&amp;namelen,&amp;basename, <u><span
style='color:blue'>NULL</span></u>)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>718</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>719</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>720</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>721</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>722</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>723</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>permission</span></u>(dir,<u><span style='color:blue'>MAY_WRITE</span></u>))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>724</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>725</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>726</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后根据指定目录的<span
lang=EN-US>i</span>节点和目录名利用函数<span lang=EN-US>find_entry()</span>寻找对应目录项，并返回包含该</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录项的缓冲块指针<span
lang=EN-US>bh</span>、包含该目录项的目录的<span lang=EN-US>i</span>节点指针<span lang=EN-US>dir</span>和该目录项指针<span
lang=EN-US>de</span>。再根据</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>该目录项<span lang=EN-US>de
</span>中的<span lang=EN-US>i</span>节点号利用<span lang=EN-US> iget()</span>函数得到对应的<span
lang=EN-US>i</span>节点<span lang=EN-US> inode</span>。如果对应路径名上最</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>后目录名的目录项不存在，则释放包含该目录项的高速缓冲区，放回目录的
<span lang=EN-US>i</span>节点，返回文</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>件已经存在出错码，并退出。如果取目录项的<span
lang=EN-US>i</span>节点出错，则放回目录的 <span lang=EN-US>i</span>节点，并释放含</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>有目录项的高速缓冲区，返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>727</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>728</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>729</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>730</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>731</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>732</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(inode = <u><span
style='color:blue'>iget</span></u>(dir-&gt;i_dev, de-&gt;inode))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>733</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>734</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>735</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>736</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>此时我们已有包含要被删除目录项的目录<span
lang=EN-US>i</span>节点<span lang=EN-US>dir</span>、要被删除目录项的<span lang=EN-US>i</span>节点<span
lang=EN-US>inode</span>和要</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>被删除目录项指针<span
lang=EN-US>de</span>。下面我们通过对这<span lang=EN-US>3</span>个对象中信息的检查来验证删除操作的可行性。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>若该目录设置了受限删除标志并且进程的有效用户<span
lang=EN-US>id</span>（<span lang=EN-US>euid</span>）不是<span lang=EN-US>root</span>，并且进程的<span
lang=EN-US>euid</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>不等于该<span lang=EN-US>i</span>节点的用户<span
lang=EN-US>id</span>，并且进程的<span lang=EN-US>euid</span>也不等于目录<span lang=EN-US>i</span>节点的用户<span
lang=EN-US>id</span>，则表示当前进</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>程没有权限删除该目录，于是放回包含要删除目录名的目录<span
lang=EN-US>i</span>节点和该要删除目录的<span lang=EN-US>i</span>节点，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后释放高速缓冲块，返回出错码。</p>

<p class=a><u><span lang=EN-US style='color:blue'>737</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((dir-&gt;i_mode
&amp; <u><span style='color:blue'>S_ISVTX</span></u>) &amp;&amp; !<u><span
style='color:blue'>suser</span></u>() &amp;&amp;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>738</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>current</span></u>-&gt;euid != inode-&gt;i_uid
&amp;&amp;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>739</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>current</span></u>-&gt;euid != dir-&gt;i_uid) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>740</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>741</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>742</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>743</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>744</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果该指定文件名是一个目录，则也不能删除。放回该目录<span
lang=EN-US>i</span>节点和该文件名目录项的<span lang=EN-US>i</span>节</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>点，释放包含该目录项的缓冲块，返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>745</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (<u><span
style='color:blue'>S_ISDIR</span></u>(inode-&gt;i_mode)) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>746</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>747</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>748</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>749</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>750</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>如果该<span lang=EN-US>i</span>节点的链接计数值已经为<span
lang=EN-US>0</span>，则显示警告信息，并修正其为<span lang=EN-US>1</span>。</p>

<p class=a><u><span lang=EN-US style='color:blue'>751</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(!inode-&gt;i_nlinks) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>752</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u><span
style='color:blue'>printk</span></u>(<i>&quot;Deleting nonexistent file
(%04x:%d), %d\n&quot;</i>,</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>753</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_dev,inode-&gt;i_num,inode-&gt;i_nlinks);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>754</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks=1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>755</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们可以删除文件名对应的目录项了。于是将该文件名目录项中的<span
lang=EN-US>i</span>节点号字段置为<span lang=EN-US>0</span>，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>表示释放该目录项，并设置包含该目录项的缓冲块已修改标志，释放该高速缓冲块。</p>

<p class=a><u><span lang=EN-US style='color:blue'>756</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-&gt;inode = 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>757</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>758</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后把文件名对应<span
lang=EN-US>i</span>节点的链接数减<span lang=EN-US>1</span>，置已修改标志，更新改变时间为当前时间。最后放</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>回该<span lang=EN-US>i</span>节点和目录的<span
lang=EN-US>i</span>节点，返回<span lang=EN-US>0</span>（成功）。如果是文件的最后一个链接，即<span
lang=EN-US>i</span>节点链接</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>数减<span lang=EN-US>1</span>后等于<span
lang=EN-US>0</span>，并且此时没有进程正打开该文件，那么在调用<span lang=EN-US>iput()</span>放回<span
lang=EN-US>i</span>节点时，该文</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>件也将被删除，并释放所占用的设备空间。参见<span
lang=EN-US>fs/inode.c</span>，第<span lang=EN-US>183</span>行。</p>

<p class=a><u><span lang=EN-US style='color:blue'>759</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>760</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>761</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_ctime =
<u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>762</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>763</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>764</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>765</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>766</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>建立符号链接。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>为一个已存在文件创建一个符号链接（也称为软连接<span
lang=EN-US> - hard link</span>）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>oldname
- </span>原路径名；<span lang=EN-US>newname - </span>新的路径名。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：若成功则返回<span
lang=EN-US>0</span>，否则返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>767</span></u><span
lang=EN-US> int <u><span style='color:blue'>sys_symlink</span></u>(const char *
oldname, const char * newname)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>768</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>769</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>770</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * dir, * inode;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>771</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh, * name_block;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>772</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>773</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int namelen, i;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>774</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char c;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>775</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先查找新路径名的最顶层目录的<span
lang=EN-US>i</span>节点<span lang=EN-US>dir</span>，并返回最后的文件名及其长度。如果目录的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // i</span>节点没有找到，则返回出错号。如果新路径名中不包括文件名，则放回新路径名目录的<span
lang=EN-US>i</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>节点，返回出错号。另外，如果用户没有在新目录中写的权限，则也不能建立连接，于是放</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>回新路径名目录的<span
lang=EN-US>i</span>节点，返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>776</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir = <u><span
style='color:blue'>dir_namei</span></u>(newname,&amp;namelen,&amp;basename, <u><span
style='color:blue'>NULL</span></u>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>777</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!dir)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>778</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EACCES</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>779</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>780</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>781</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>782</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>783</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>permission</span></u>(dir,<u><span style='color:blue'>MAY_WRITE</span></u>))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>784</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>785</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EACCES</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>786</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们在目录指定设备上申请一个新的<span
lang=EN-US>i</span>节点，并设置该<span lang=EN-US>i</span>节点模式为符号链接类型以及</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>进程规定的模式屏蔽码。并且设置该<span
lang=EN-US>i</span>节点已修改标志。</p>

<p class=a><u><span lang=EN-US style='color:blue'>787</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(inode = <u><span
style='color:blue'>new_inode</span></u>(dir-&gt;i_dev))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>788</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>789</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>790</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>791</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_mode = <u><span
style='color:blue'>S_IFLNK</span></u> | (0777 &amp; ~current-&gt;<u><span
style='color:blue'>umask</span></u>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>792</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>为了保存符号链接路径名字符串信息，我们需要为该<span
lang=EN-US>i</span>节点申请一个磁盘块，并让<span lang=EN-US>i</span>节点的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>第<span lang=EN-US>1</span>个直接块号<span
lang=EN-US>i_zone[0]</span>等于得到的逻辑块号。然后置<span lang=EN-US>i</span>节点已修改标志。如果申请失败</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则放回对应目录的<span
lang=EN-US>i</span>节点；复位新申请的<span lang=EN-US>i</span>节点链接计数；放回该新的<span
lang=EN-US>i</span>节点，返回没有空</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>间出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>793</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(!(inode-&gt;i_zone[0]=<u><span style='color:blue'>new_block</span></u>(inode-&gt;i_dev)))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>794</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>795</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>796</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>797</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>798</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>799</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后从设备上读取新申请的磁盘块（目的是把对应块放到高速缓冲区中）。若出错，则放回</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>对应目录的<span lang=EN-US>i</span>节点；复位新申请的<span
lang=EN-US>i</span>节点链接计数；放回该新的<span lang=EN-US>i</span>节点，返回没有空间出错</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>800</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(name_block=<u><span
style='color:blue'>bread</span></u>(inode-&gt;i_dev,inode-&gt;i_zone[0]))) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>801</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>802</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>803</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>804</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ERROR</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>805</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们可以把符号链接名字符串放入这个盘块中了。盘块长度为<span
lang=EN-US>1024</span>字节，因此默认符号</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>链接名长度最大也只能是<span
lang=EN-US>1024</span>字节。我们把用户空间中的符号链接名字符串复制到盘块所在</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的缓冲块中，并置缓冲块已修改标志。为防止用户提供的字符串没有以<span
lang=EN-US>null</span>结尾，我们在缓</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>冲块数据区最后一个字节处放上一个<span
lang=EN-US>NULL</span>。然后释放该缓冲块，并设置<span lang=EN-US>i</span>节点对应文件中数</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>据长度等于符号链接名字符串长度，并置<span
lang=EN-US>i</span>节点已修改标志。</p>

<p class=a><u><span lang=EN-US style='color:blue'>806</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>807</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (i &lt; 1023
&amp;&amp; (c=<u><span style='color:blue'>get_fs_byte</span></u>(oldname++)))</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>808</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
name_block-&gt;b_data[i++] = c;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>809</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
name_block-&gt;b_data[i] = 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>810</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
name_block-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>811</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(name_block);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>812</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_size =
i;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>813</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inode-&gt;i_dirt =
1;</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后我们搜索一下路径名指定的符号链接文件名是否已经存在。若已经存在则不能创建同名</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>目录项<span lang=EN-US>i</span>节点。如果对应符号链接文件名已经存在，则释放包含该目录项的缓冲区块，复位</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>新申请的<span lang=EN-US>i</span>节点连接计数，并放回目录的<span
lang=EN-US>i</span>节点，返回文件已经存在的出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>814</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>815</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>816</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>817</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>818</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>819</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>820</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EEXIST</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>821</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在我们在指定目录中新添加一个目录项，用于存放新建符号链接文件名的<span
lang=EN-US>i</span>节点号和目录</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>名。如果失败（包含该目录项的高速缓冲区指针为<span
lang=EN-US>NULL</span>），则放回目录的<span lang=EN-US>i</span>节点；所申请的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // i</span>节点引用连接计数复位，并放回该<span
lang=EN-US>i</span>节点。返回出错码退出。</p>

<p class=a><u><span lang=EN-US style='color:blue'>822</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>add_entry</span></u>(dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>823</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>824</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inode-&gt;i_nlinks--;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>825</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>826</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>827</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>828</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>最后令该新目录项的<span
lang=EN-US>i</span>节点字段等于新<span lang=EN-US>i</span>节点号，并置高速缓冲块已修改标志，释放高速缓</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>冲块，放回目录和新的<span
lang=EN-US>i</span>节点，最后返回<span lang=EN-US>0</span>（成功）。</p>

<p class=a><u><span lang=EN-US style='color:blue'>829</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-&gt;inode =
inode-&gt;i_num;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>830</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>831</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>832</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>833</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(inode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>834</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>835</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>836</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; //// </span>为文件建立一个文件名目录项。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>为一个已存在的文件创建一个新链接（也称为硬连接<span
lang=EN-US> - hard link</span>）。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>参数：<span lang=EN-US>oldname
- </span>原路径名；<span lang=EN-US>newname - </span>新的路径名。</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>返回：若成功则返回<span
lang=EN-US>0</span>，否则返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>837</span></u><span
lang=EN-US> int <u><span style='color:blue'>sys_link</span></u>(const char *
oldname, const char * newname)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>838</span></u><span
lang=EN-US> {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>839</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>dir_entry</span></u> * de;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>840</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>m_inode</span></u> * oldinode, * dir;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>841</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct <u><span
style='color:blue'>buffer_head</span></u> * bh;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>842</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *
basename;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>843</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int namelen;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>844</span></u><span
lang=EN-US> </span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>首先对原文件名进行有效性验证，它应该存在并且不是一个目录名。所以我们先取原文件路</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>径名对应的<span lang=EN-US>i</span>节点<span
lang=EN-US>oldinode</span>。如果为<span lang=EN-US>0</span>，则表示出错，返回出错号。如果原路径名对应的是</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>一个目录名，则放回该<span
lang=EN-US>i</span>节点，也返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>845</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; oldinode=<u><span
style='color:blue'>namei</span></u>(oldname);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>846</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!oldinode)</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>847</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOENT</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>848</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (<u><span
style='color:blue'>S_ISDIR</span></u>(oldinode-&gt;i_mode)) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>849</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>850</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>851</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>然后查找新路径名的最顶层目录的<span
lang=EN-US>i</span>节点<span lang=EN-US>dir</span>，并返回最后的文件名及其长度。如果目录的</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // i</span>节点没有找到，则放回原路径名的<span
lang=EN-US>i</span>节点，返回出错号。如果新路径名中不包括文件名，</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>则放回原路径名<span
lang=EN-US>i</span>节点和新路径名目录的<span lang=EN-US>i</span>节点，返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>852</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir = <u><span
style='color:blue'>dir_namei</span></u>(newname,&amp;namelen,&amp;basename, <u><span
style='color:blue'>NULL</span></u>);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>853</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!dir) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>854</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>855</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EACCES</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>856</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>857</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!namelen) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>858</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>859</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>860</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EPERM</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>861</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>我们不能跨设备建立硬链接。因此如果新路径名顶层目录的设备号与原路径名的设备号</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>不一样，则放回新路径名目录的<span
lang=EN-US>i</span>节点和原路径名的<span lang=EN-US>i</span>节点，返回出错号。另外，如果用户</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>没有在新目录中写的权限，则也不能建立连接，于是放回新路径名目录的<span
lang=EN-US>i</span>节点和原路径名</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>的<span lang=EN-US>i</span>节点，返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>862</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dir-&gt;i_dev
!= oldinode-&gt;i_dev) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>863</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>864</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>865</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EXDEV</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>866</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>867</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!<u><span
style='color:blue'>permission</span></u>(dir,<u><span style='color:blue'>MAY_WRITE</span></u>))
{</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>868</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>869</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>870</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EACCES</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>871</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在查询该新路径名是否已经存在，如果存在则也不能建立链接。于是释放包含该已存在目</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>录项的高速缓冲块，放回新路径名目录的<span
lang=EN-US>i</span>节点和原路径名的<span lang=EN-US>i</span>节点，返回出错号。</p>

<p class=a><u><span lang=EN-US style='color:blue'>872</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>find_entry</span></u>(&amp;dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>873</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>874</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>875</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>876</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>877</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>EEXIST</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>878</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>现在所有条件都满足了，于是我们在新目录中添加一个目录项。若失败则放回该目录的<span
lang=EN-US>i</span>节</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>点和原路径名的<span
lang=EN-US>i</span>节点，返回出错号。否则初始设置该目录项的<span lang=EN-US>i</span>节点号等于原路径名的<span
lang=EN-US>i</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>节点号，并置包含该新添目录项的缓冲块已修改标志，释放该缓冲块，放回目录的<span
lang=EN-US>i</span>节点。</p>

<p class=a><u><span lang=EN-US style='color:blue'>879</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh = <u><span
style='color:blue'>add_entry</span></u>(dir,basename,namelen,&amp;de);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>880</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!bh) {</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>881</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>882</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<u><span style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>883</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return -<u><span style='color:blue'>ENOSPC</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>884</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>885</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-&gt;inode =
oldinode-&gt;i_num;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>886</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bh-&gt;b_dirt = 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>887</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>brelse</span></u>(bh);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>888</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(dir);</span></p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>再将原节点的链接计数加<span
lang=EN-US>1</span>，修改其改变时间为当前时间，并设置<span lang=EN-US>i</span>节点已修改标志。最后</p>

<p class=a><span lang=EN-US>&nbsp;&nbsp;&nbsp; // </span>放回原路径名的<span
lang=EN-US>i</span>节点，并返回<span lang=EN-US>0</span>（成功）。</p>

<p class=a><u><span lang=EN-US style='color:blue'>889</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
oldinode-&gt;i_nlinks++;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>890</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
oldinode-&gt;i_ctime = <u><span style='color:blue'>CURRENT_TIME</span></u>;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>891</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; oldinode-&gt;i_dirt
= 1;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>892</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <u><span
style='color:blue'>iput</span></u>(oldinode);</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>893</span></u><span
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>894</span></u><span
lang=EN-US> }</span></p>

<p class=a><u><span lang=EN-US style='color:blue'>895</span></u><span
lang=EN-US> </span></p>

<div class=a align=center style='text-align:center'><span lang=EN-US>

<hr size=4 width="100%" align=center>

</span></div>

<p class=MsoNormal><span lang=EN-US>&nbsp;</span></p>

<p class=MsoNormal><span lang=EN-US>&nbsp;</span></p>

</div>

</body>

</html>
